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.

Shantha Kumar
Shantha Kumar
Articles: 296

24,849 Comments

  1. 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();
    }

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

  3. gettig below error.

    Microsoft.SharePoint.SPException: One or more field types are not installed properly. Go to the list settings page to delete these fields.

    what to do?

  4. Hi

    Good post . I am trying to streamline at Get-PnPListItem call to bring all files from a SharePoint online Library and recursively get the files from any folders whilst ignoring the folders themselves

    When I get an item I can interrogate the type so I wonder if I need to use this in my CAML
    $i.FileSystemObjectType
    Folder

    This doesn’t appear to work:
    “1”

Comments are closed.