4

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.

Site
|-List
|  |-Folder
|  |-Folder
|     |-Folder
|
|-List
|  |-Folder
|
|-List

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));
                else
                    child = new TreeNode(item.Name);

                // Collect folders
                branchList.Add(child);
            }
            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.

TreeView

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?

4
  • 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, 2012 at 19:06
  • 1
    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, 2012 at 8:08
  • @gandjustas Thanks, you where right. I've updated the code.
    – Amicable
    Jul 9, 2012 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, 2012 at 10:04

2 Answers 2

2

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.

1
  • Just came in useful again for me again, can't vote it up twice unfortunately!
    – Amicable
    Feb 13, 2013 at 17:08
1

I found this article while searching for a function to get all folders of a certain document library as a tree. Due to some errors in the recursive method shown by Amicable I rewrote the method. May be it is usefull for others:

The recursive function:

private void CreateBranch(SPFolder folder, TreeNode tnParent)
{
    if (folder.SubFolders.Count > 0)
    {
        foreach (SPFolder item in folder.SubFolders)
        {
            TreeNode child = new TreeNode(item.Name);

            if (item.SubFolders.Count > 0)
            {
                // Start recursion
                CreateBranch(item, child);
            }
            tnParent.ChildNodes.Add(child);
        }
    }
}

Call the function:

SPFolder listRootFolder = SPContext.Current.Web.Lists["Shared Documents"].RootFolder;
TreeNode parentNode = new TreeNode("root");
CreateBranch(listRootFolder, parentNode);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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