Creating a Tree View Control WebPart |
I had a requirement to create a tree view control to rollup document libraries into a single view. I chose to use the System.Web.UI.WebControls.TreeView web control.
Once you define the tree view control you add nodes, for example:
TreeNode childNode = new TreeNode(file.Name, "", "~/_layouts/images/" + file.IconUrl, file.ServerRelativeUrl.ToString(), "");
treeView.ChildNodes.Add(childNode);
I wanted to get all document libraries for a current web, I used the SPWeb.GetListsOfType method, this takes in a SPBaseType enumeration. For example:
currentWeb.GetListsOfType(SPBaseType.DocumentLibrary)
I wrote a recursive function which loops through all the root document libraries and loops through their folders. This looked like:
foreach(SPList list in currentWeb.GetListsOfType(SPBaseType.DocumentLibrary))
{
// build the tree
rootNode = new System.Web.UI.WebControls.TreeNode(list.Title, "", "~/_layouts/images/itdl.gif", list.RootFolder.ServerRelativeUrl.ToString(), "");
// loop down the tree
TraverseFolder(list.RootFolder, rootNode);
// add the root node to tree view
treeView.Nodes.Add(rootNode);
}
The completed webpart looks like:
Here's the code: