Removing checkbox from SharePoint view using PowerShell

At times there is a need to remove the selection checkboxes in SharePoint list/library views. The following PowerShell code does that for you $OpenWeb = Get-SPWeb http://mysite.cloudapp.net
$OpenList = $OpenWeb.Lists["My Library"]
$OpenView = $OpenList.Views["All Documents"]
$OpenView.TabularView = $false
$OpenView.Update()

Show custom error message instead of Web part access denied – SharePoint 2010

Many times while using “ListViewWebPart” you get the following error if the user does not have access to the list/library
Error Web Part Error: Access denied. You do not have permission to perform this action or access this resource”
clip_image00f2
Though the error is kind of explanatory but is not very user friendly. I have getting demands from lot of customers to replace this error with a custom error message. Let us see how we can achieve this using Jquery
1. Download latest version of Jquery
2. Edit Page where you have added you ListViewWebPart and add a “Content Editor Web part” at the end of the page. Make the web part hidden
3. Add the following JavaScript to the “Content Editor Web Part”

<script type="text/javascript" src="/Shared%20Document/jquery.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('div[errorwebpart="1"]').text('The content of this section is restricted to authorized users only.');
$('div[errorwebpart="1"]').addClass("ms-vb2");
}
);
</script>

Note: the path for the Jquery should be your path for the Jquery file

Once done you will be able to see the custom message instead of generic Sharepoint message

clip_image00v4

Create listview webpart to show items inside folders

When you navigate inside a folder inside a document library you will notice that the URL changes. SharePoint makes use of URL Querystring to display data inside folders.

Assume you have document library as “Shared Documents” and you create a folder called as “mydocument” inside it. when you navigate inside the folder the url in your browser should look something like this

http://{yoursite}/Shared%20Documents/Forms/AllItems.aspx?RootFolder=/Shared%20Documents/mydocument&FolderCTID=&View={A4A4A2E6-AE49-4173-B738-A14A4A1A49A0}

As you may see that the SharePoint OOTB list view webpart uses these three parameters (RootFolder,FolderCTID,View) to render the folder contents to you

RootFolder – Root Folder URL
FolderCTID – Folder Content Type ID (optional)
View – Specified View Guid for the Document library

So you need to create a custom list view web part and then set the following three properties to show data inside a specific folder

But there is a catch here. You can set the View for a listview web part but not the “RootFolder” and “FolderCTID” as they are managed internally by sharepoint.Here we will use refelection to set these values and achieve desired results

Code Sample

protected override void CreateChildControls()
    {
      base.CreateChildControls();
      try
      {
        SPSite site = new SPSite("http://{yoursite}");
        SPWeb web = site.OpenWeb();
        SPList list = web.Lists["Shared Documents"];

        // Instantiate the web part
        ListViewWebPart lvwp = new ListViewWebPart();
        lvwp.ListName = list.ID.ToString("B").ToUpper();
        lvwp.ViewGuid = list.Views["SomeView"].ID.ToString("B").ToUpper();
              
        //Get the rootfolder url here and content type id of folder e.g. "0x012000C5E1253B0EF534479884B846FA62FCFC"
      
       SPFolder folder = web.GetFolder("/Shared Documents/mydocument");

       SPContentTypeId folderctid = folder.Item.ContentType.Id;

        //set the folder url
        SetPrivateFieldValue(lwwp,"rootFolder",folder.Url);
        
       // set the folder content type id ----- Note this is optional and can be skipped 🙂 
       SetPrivateFieldValue(lwwp,"folderCtId",folderctid.ToString());

        lvwp.GetDesignTimeHtml();
        this.Controls.Add(lvwp);
      }
      catch (Exception ex)
      {
        Label lbl = new Label();
        lbl.Text = "Error occured: ";
        lbl.Text += ex.Message;
        this.Controls.Add(lbl);
      }
    }

private static void SetPrivateFieldValue(object obj, string fieldName,string val)
{
  FieldInfo fi = obj.GetType().GetField(fieldName,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  fi.SetValue(obj, val);
}