Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've trying to build a tree view which will show a Sites Lists and folders. This is an external application so I have to communicate with WSS3 through web services.

|  |-Folder
|  |-Folder
|     |-Folder
|  |-Folder

I was able to drill down from my SPWeb.Lists object to get the SPListCollection. However from the SPList.Folders collection you get the SPListItemCollection, not SPFolderCollection. There doesn't appear to be a property for child objects in the SPList object.

As you can't cast SPListItem to SPFolder I got the SPFolderCollection directly from SPWeb.Folders instead but this seems to return odd things.

I have a List called "Developer" which contains a folder called "First" that has a subfolder called "Deeper".

This is the recursive method I wrote to create nodes for the folders.

    private TreeNode[] CreateBranch(SPFolder folder)
        if (folder.SubFolders.Count > 0)
            List<TreeNode> branchList = new List<TreeNode>();

            TreeNode child;
            foreach (SPFolder item in folder.SubFolders)
                if (item.SubFolders.Count > 0)
                    // Start recursion
                    child = new TreeNode(item.Name, CreateBranch(item));
                    child = new TreeNode(item.Name);

                // Collect folders
            return branchList.ToArray();
        return null;

This builds me a tree, as it should, but SubFolders of Folders do not seem to appear in the collection as a looped through it at all. The "Developer" list also came up as a folder, as well as the web directories.


This has left me a little confused at which collection I should be using and how I am supposed to tell if a folder has subfolders at all. Can anyone help point me in the right direction?

share|improve this question
All of this code is moot if you will have to use the web services instead of the SharePoint API. Can you clarify? –  Kit Menke Jul 6 '12 at 19:06
There is an error in code. You don't use recursive call results. If you call CreateBranch for every folder in Web then it return only one-level-deep folders. –  gandjustas Jul 7 '12 at 8:08
@gandjustas Thanks, you where right. I've updated the code. –  Amicable Jul 9 '12 at 13:45
@KitMenke ohh, I see what you meant now. It won't work outside the development environment. I guess I'll have to create my own objects. –  Amicable Jul 12 '12 at 10:04

1 Answer 1

up vote 2 down vote accepted

This is note quite an answer, but more a comment that didn't fit the space allowed for comments...

It looks like you want to display the lists and then the folders in those lists. That's not the same as displaying the folders in a site.

SPWeb.Folders returns the list of folders in a SharePoint site's root. Think of those as 'url-paths' that exist on the site. Your 'Developer' list (a document library I assume?) is accessible as http://site/Developer/, so that's why it appears as both a list and a folder. SPFolder.SubFolders should return lower level folders.

You're probably after the folders in a particular list. SPList.Folders returns all the folders in a list. To a list, a folder is just an item with content type 'Folder', so that's why getting it returned as a SPListItemCollection is not so strange. Each SPListItem has an SPFolder property, but I don't think you need that. Note that SPList.Folders returns all folders in the list (recursively).

As pointed out above by Kit Menke, your code seems to use the server object model, getting the information from webservices is not quite the same, but it will help to understand the object model first and use it to figure out what you want to display.

share|improve this answer
Just came in useful again for me again, can't vote it up twice unfortunately! –  Amicable Feb 13 '13 at 17:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.