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


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()
        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
       // set the folder content type id ----- Note this is optional and can be skipped 🙂 

      catch (Exception ex)
        Label lbl = new Label();
        lbl.Text = "Error occured: ";
        lbl.Text += ex.Message;

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);

6 thoughts on “Create listview webpart to show items inside folders”

  1. In Sharepoint 2010, rootFolder is no more Field.

    They have changed it to Property. Hence in case of Sharepoint 2010 SetPrivateFieldValue method should be looking for PropertyInfo instead of FieldInfo

  2. Great job! But one point… ListViewWebPart does not like cross Site/Web reference. It works right only if the list is inside a current web…what a pitty

  3. Typically this error occurs when you try to access some collection in Sharepoint and the item is not present in the collection.

    SPWeb.Lists["Shared Documents"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.