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.


ravikiran · September 14, 2009 at 12:30 PM

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.


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: " +
+ " Folder: " +
oFolder.Name + " Number: "
+ oFolder.Files.Count +
" Size: " + lngTotalFileSize + "”;

Kishore · February 19, 2010 at 7:14 AM

Nice Article. Helped me alot. Thanks.

Kishore · February 19, 2010 at 7:15 AM

Really Very Useful for beginners.Works fine.

Madhu Shekar · October 4, 2010 at 12:27 PM

Very nice article. Thanks for ur post

mousavi · March 7, 2011 at 6:03 PM

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

Thomas · April 5, 2012 at 8:25 PM

Thanks! That helped a lot!

bharath · November 15, 2012 at 10:10 AM

Thanks a lot.. :)

akash · February 5, 2016 at 8:54 PM

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?

Daniel Westerdale · May 12, 2017 at 6:38 PM


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

This doesn’t appear to work:

Leave a Reply

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