Click here to Skip to main content
15,512,456 members
Articles / Web Development / ASP.NET
Posted 20 Sep 2009

Tagged as


9 bookmarked

Tree View WebPart for Sharepoint List Data

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
20 Sep 2009CPOL1 min read
A treeview WebPart for Sharepoint list data.


This article provides you with the code snippet to display a SharePoint list data as a tree view WebPart.


The code creates a tree view control and then populates it with various folders and subfolders from a given list. To access the subfolders of the list, an SPQuery variable is used. The list item is first checked with a condition (listitem.Folder == null) to determine whether it is a folder or a list item. The GetItems recursive method is then called to get the list item or the items in the folder.

Using the code

The code displays data from a "Links" type custom list called "All Programs" as a tree view. The CreateChildControl method calls the GetFolder method to access the subfolders of the list. The GetFolder method further calls the GetItems method to get the items or subfolders in a subfolder. GetItems uses SPquery to get the items in a folder and is called recursively to get the items in subfolders of a folder.

public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    TreeView treeView;
    TreeNode rootNode;

    public WebPart1()

    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        // render the control

    protected override void CreateChildControls()

        // get the current site
        SPSite currentSite = SPContext.Current.Site;
        using (SPWeb currentWeb = currentSite.OpenWeb())
            // set the tree view properties
            treeView = new System.Web.UI.WebControls.TreeView();
            treeView.ShowLines = true; // show lines
            treeView.ExpandDepth = 0; // expand non

            SPList list = currentWeb.Lists["All Programs"];

            // build the tree
            rootNode = new System.Web.UI.WebControls.TreeNode(list.Title, "", 
                       "", list.RootFolder.ServerRelativeUrl.ToString(), "");

            // loop down the tree
            GetFolder(list.RootFolder, rootNode, list);

            // add the root node to tree view

    public void GetFolder(SPFolder folder, TreeNode rootNode, SPList list)
        // create a new node
        TreeNode newNode = new System.Web.UI.WebControls.TreeNode(folder.Name, "", 
                           folder.ServerRelativeUrl.ToString(), "");
            // don't add the forms folder
            if (folder.Name != "Link")
                // loop through all child folders
                foreach (SPFolder childFolder in folder.SubFolders)
                    // don't add the forms folder
                    if (childFolder.Name != "Link")
                        TreeNode trn = 
                          new System.Web.UI.WebControls.TreeNode(childFolder.Name, "", 
                          "", childFolder.ServerRelativeUrl.ToString(), "");
                        newNode = GetItems(childFolder, trn);

                        // add the new node to the tree
        catch { }

    public TreeNode GetItems(SPFolder folder, TreeNode node)
        //Get Items from childFolder
        SPQuery qry = new SPQuery();
        qry.Folder = folder;
        SPWeb web = null;

        web = folder.ParentWeb;
        SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);

        foreach (SPListItem subitem in ic)
            if (subitem.Folder != null) //Is Subfolder
                // create a new node for a subfolder and add items to it
                TreeNode childNode = 
                  new System.Web.UI.WebControls.TreeNode(subitem.Folder.Name);
                node.ChildNodes.Add(GetItems(subitem.Folder, childNode));

            if (subitem.Folder == null)
                TreeNode trn1 = new System.Web.UI.WebControls.TreeNode(
        return node;

In the above code, SPquery is used to get the items from a given folder since there is no other way to get items from a list of subfolders as item.folder does not expose subfolders.

For any comments and questions on this article, please visit my blog:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionerror when I add web part Pin
Tin Nay Lar18-Apr-12 0:16
Tin Nay Lar18-Apr-12 0:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.