Tuesday, March 31, 2009

Iterating through All the Folders in your Sharepoint Site

Recently I got a request from a client to get a report of the number of items they had in the document libraries on their site. They wanted it broken down with the number of files per site, document library, and eventually per folder. Anyways its a fairly simple procedure (or so I think anyways), but figured I’d post it up since its been a while since I’ve thrown anything up. The code is broken into two methods, the first is used to iterate through all the child webs of a given SPWeb and also iterates through all the lists for that web and then calls the second method to interate through all the folders in a given list.

public void CrawlWeb(SPWeb web)
{
foreach (SPList list in web.Lists)
{
CrawlFolder(list.RootFolder);
}

foreach (SPWeb subWeb in web.Webs)
{
CrawlWeb(subWeb);
}
}

public void CrawlFolder(SPFolder folder)
{
if (folder != null && folder.Name.CompareTo(“forms”) != 0)
{
foreach (SPFolder subFolder in folder.SubFolders)
{
CrawlFolder(subFolder);
}
}
}

No comments: