tags:

views:

471

answers:

2

Using the following code:

using (SPSite site = new SPSite("http://localhost/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        foreach (SPList list in web.Lists)
        {
            if (list.OnQuickLaunch)
            {
                Console.WriteLine(list.Title);

                foreach (SPListItem item in list.Folders)
                {
                    Console.WriteLine("- " + item.Title);
                }
            }
        }
    }
}

and the output:

... various lists ...
Shared Documents
- Minutes
- Second Level

I get all the folders back as a flat list - no real indication of the nesting that can happen as a folder is created as a child of another folder. Spelunking around with Visual Studio I can see a few interesting properties that might give me some clues (like item.Url and counting / characters or item.Folder.ParentFolder compared against something?), but there has got to be a simpler way.

Thanks!

+1  A: 

You may like to check out the following question for answers. While it is not the same question, the underlying problem is similar.

Should I create my own object model.

Nat
+3  A: 

You can traverse the folder hierarchy from the root folder of any list, i.e. list.RootFolder. Having the root SPFolder object, you can start traversing the hierarchy recursively using the SubFolders property on each SPFolder object.

Lars Fastrup