Retrieve all folders with sub-folders from SharePoint List

In this post, I’ll show you how to get all the folders including subfolders from Document Library or List in sharepoint.

This is just easy, by using SPQuery object and setting ViewAttributes property in it; we can retrieve only the folders from the SharePoint List, with a single condition. Now I’ll show you a code,

SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;

SPList list = web.Lists[“Shared Documents”];

SPQuery query = new SPQuery();

//Condition to check the item type is folder or not
query.Query = “<Where><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></Where>”;

//Get all the items including subfolders from the list query.ViewAttributes = “Scope=’RecursiveAll'”;

//Retrieve the items based on Query SPListItemCollection items = list.GetItems(query);

string folderDetails=“”;

//Get the name and Url for the folder
foreach (SPListItem item in items)
{
folderDetails += “Folder Name:” + item.Name + “<br/>Folder URL:” + web.Url + “/” + item.Url + “<br/>”;
}

In Query property of SPQuery object, we have to set the condition of “FSObjType” is equal to 0, this is the “Item Type” value for the folder, the list items or documents contain the Item Type value as 1.

And then Scope=’RecursiveAll’ is nothing but to retrieve all the items and folders from the list or library.

I’ll hope this post help you all about retrieving all the folders from the SharePoint List or Library.

7 thoughts on “Retrieve all folders with sub-folders from SharePoint List

  1. ravikiran

    Hi Shanta,
    I am using this code however it gives error,even I have tried with default site also there is no output.
    Please help me with this.

    http://servername

    SPSite site = new SPSite(“http://docholder:41653/”);
    SPWebCollection collWebsites = site.AllWebs;
    foreach (SPWeb oWebsite in collWebsites)
    {
    SPFolderCollection collFolders = oWebsite.Folders;

    foreach (SPFolder oFolder in collFolders)
    {
    SPFileCollection collFiles = oFolder.Files;

    long lngTotalFileSize = 0;

    for (int intIndex = 0; intIndex < collFiles.Count; intIndex++)
    {
    lngTotalFileSize += collFiles[intIndex].Length;
    }

    Label1.Text += " Web: " +
    oWebsite.Name
    + " Folder: " +
    oFolder.Name + " Number: "
    + oFolder.Files.Count +
    " Size: " + lngTotalFileSize + "”;
    }
    oWebsite.Dispose();
    }

    Reply
  2. mousavi

    XElement queryOptions = new XElement(“QueryOptions”,
    new XElement(“ViewAttributes”, new XAttribute(“Scope”, “RecursiveAll”)),
    new XElement(“IncludeMandatoryColumns”, “False”));
    XElement query = new XElement(“Query”,
    // new XElement(“OrderBy”, new XElement(“FieldRef”, new XAttribute(“Name”, “BaseName”), new XAttribute(“Ascending”, “True”))),
    new XElement(“Where”,
    new XElement(“Or”, new XElement(“Eq”, new XElement(“FieldRef”, new XAttribute(“Name”, “ContentType”)),
    new XElement(“Value”, new XAttribute(“Type”, “String”), “Folder”)))));
    XElement view = new XElement(“ViewFields”, new XElement(“FieldRef”, new XAttribute(“Name”, “BaseName”)));

    XElement ndListItems = ListService.Client.GetListItems(drpList.SelectedText, null, query, view, “100”, queryOptions, null);

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>