Tree View Webpart for sharepoint list

The idea was to create a tree view webpart for the data in a Custom SharePoint list.

Further, I also added a WebPart Property for Cutsom list's Name so that it becomes a generic webpart and we can use it for any custom list.

It will look about below:


Note : I am using a Link type list named "All Programs".

Below is the Code to create a tree view, bind to a list

namespace CustomNavigationWP
{
[Guid("93215435-7b75-49e7-b99f-07e96e50b02f")]
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
base.RenderContents(writer);
}

protected override void CreateChildControls()
{
base.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;

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
treeView.Nodes.Add(rootNode);

}
this.Controls.Add(treeView);
base.CreateChildControls();
}

public void GetFolder(SPFolder folder, TreeNode rootNode, SPList list)
{
// create a new node
TreeNode newNode = new System.Web.UI.WebControls.TreeNode(folder.Name, "", "~/_layouts/images/itdl.gif", folder.ServerRelativeUrl.ToString(), "");
try
{
// 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
rootNode.ChildNodes.Add(newNode);
}
}

}
}
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(subitem["Title0"].ToString());
node.ChildNodes.Add(trn1);
}
}
return node;
}}
}

8 comments:

  1. Hi I was looking to build a web part just like this but rather than display folders and the documents within the folders I want the tree viewer to display contents from an excel workbook. Is this possible? I am looking to build an online FAQ using this functionality and that will update when i make edits to the excel file source.
    SO in my spreadsheet there are 4 columns used: 1. Category, 2. sub category, 3. Question, 4. answer.

    I am very new to this and appreciate any guidance. thank you.

    ReplyDelete
  2. Hello,

    I was looking for something exactly like this. I am having a problem displaying the items in the folders or subfolders. Am I doing something wrong?

    ReplyDelete
  3. Hey,
    This looks like what I am looking for but i am kinda new at SharePoint designing and it isn't magically working. :) I would like to use this to make a tree view of my site's discussion boards. Any help you could provide would be great.

    ReplyDelete
  4. Hi,
    I am new to Sharepoint.
    Would you please tell me what should be value for "All Programs" in ones site.
    I want to use it for Site Direcotry of my Site.
    Code line :- SPList list = currentWeb.Lists["All Programs"];

    Thanks

    ReplyDelete
  5. Great webpart!

    Why dont you put it on sale and earn some bucks. Visit http://www.thesharepointmarket.com/submit-product/
    and start earning!

    ReplyDelete
  6. Hello! Good work! I have a problem displaying the items in the folders or subfolders. Any suggestions? I have Links type list. Thanks!

    ReplyDelete
  7. Hello ,
    I create a list view , I want to display such as tree view (root ,child).
    which method to use .

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com