Tuesday, February 9, 2010

List Tree View in Sharepoint


In one of our project there are more than 100 lists and every list is having nested folders as well as many items. My client don't want to dig the list to see all the nested folder inside it. So we decided to provide him the Tree View control which will show all the lists inside site and also show all the items/data inside a list and he can easily navigate to particular list/folder and item.

First we will show all the items/nested folder in TreeView

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Nishnat.ListTreeView
{
public class ListItems : WebControl
{
public string ListName { get; set; }

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

protected override void CreateChildControls()
{
base.CreateChildControls();

using (SPSite ospSite = new SPSite("http://localhost:7777/"))
{
using (SPWeb web = ospSite.RootWeb)
{
try
{
SPList list = web.Lists[ListName];

SPFolder rootFolder = list.RootFolder;
TreeView listTree = new TreeView();
listTree.ShowLines = true;
listTree.ExpandDepth = 0;

TreeNode rootNode = new TreeNode();

//Bind tree
MakeTreee(rootFolder, rootNode);

// add the root node to tree view
listTree.Nodes.Add(rootNode);
this.Controls.Add(listTree);
}
catch(Exception ex)
{
throw ex;
}
}
}
}

private void MakeTreee(SPFolder rootFolder, TreeNode rootNode)
{
SPQuery query = new SPQuery();
query.Folder = rootFolder;
SPWeb web = rootFolder.ParentWeb;
SPListItemCollection listColl = web.Lists[rootFolder.ParentListId].GetItems(query);

foreach (SPListItem subitem in listColl)
{
if (subitem.Folder != null) //Is folder
{
TreeNode childNode = new TreeNode(subitem.Folder.Name,subitem.ID.ToString(), "_layouts/images/folder.gif", subitem.Folder.ServerRelativeUrl, "");
rootNode.ChildNodes.Add(childNode);
MakeTreee(subitem.Folder, childNode);
}
else
{
string displayURL = GetDisplayUrl(subitem);

TreeNode childNode = new TreeNode(subitem.Name, subitem.Name, "", displayURL, "");
rootNode.ChildNodes.Add(childNode);
}
}
}

private static string GetDisplayUrl(SPListItem item)
{
SPList list = item.ParentList;
SPWeb web = list.ParentWeb;

string dispUrl = item.ContentType.DisplayFormUrl;
//dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (dispUrl == "")
dispUrl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;

bool isLayouts = dispUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (isLayouts)
dispUrl = String.Format("{0}&List={1}", dispUrl, SPEncode.UrlEncode(list.ID + ""));
return dispUrl;
}
}
}


Second we will show all the Lists/items/nested folder in TreeView


using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Nishnat.ListTreeView
{
public class AllListsWithItems : WebControl
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
// render the control
base.RenderContents(writer);
}

protected override void CreateChildControls()
{
base.CreateChildControls();

using (SPSite ospSite = new SPSite("http://localhost:7777/"))
{
using (SPWeb web = ospSite.RootWeb)
{
try
{
TreeView listTree = new TreeView();
listTree.ShowLines = true;
listTree.ExpandDepth = 0;

SPListCollection listAll = web.Lists;

foreach (SPList list in listAll)
{
SPFolder rootFolder = list.RootFolder;

TreeNode rootNode = new TreeNode(list.Title,list.ID.ToString(),list.ImageUrl,list.ParentWebUrl,"");

//Bind tree
MakeTreee(rootFolder, rootNode);

// add the root node to tree view
listTree.Nodes.Add(rootNode);
this.Controls.Add(listTree);
}
}
catch(Exception ex)
{
throw ex;
}
}
}
}

private void MakeTreee(SPFolder rootFolder, TreeNode rootNode)
{
SPQuery query = new SPQuery();
query.Folder = rootFolder;
SPWeb web = rootFolder.ParentWeb;
SPListItemCollection listColl = web.Lists[rootFolder.ParentListId].GetItems(query);

foreach (SPListItem subitem in listColl)
{
if (subitem.Folder != null) //Is folder
{
TreeNode childNode = new TreeNode(subitem.Folder.Name,subitem.ID.ToString(), "_layouts/images/folder.gif", subitem.Folder.ServerRelativeUrl, "");
rootNode.ChildNodes.Add(childNode);
MakeTreee(subitem.Folder, childNode);
}
else
{
string displayURL = GetDisplayUrl(subitem);

TreeNode childNode = new TreeNode(subitem.Name, subitem.Name, "", displayURL, "");
rootNode.ChildNodes.Add(childNode);
}
}
}

private static string GetDisplayUrl(SPListItem item)
{
SPList list = item.ParentList;
SPWeb web = list.ParentWeb;

string dispUrl = item.ContentType.DisplayFormUrl;
//dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (dispUrl == "")
dispUrl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;

bool isLayouts = dispUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
dispUrl = String.Format("{0}/{1}?ID={2}", web, dispUrl, item.ID);

if (isLayouts)
dispUrl = String.Format("{0}&List={1}", dispUrl, SPEncode.UrlEncode(list.ID + ""));
return dispUrl;
}
}
}


* Add assembly into GAC and Bin application

* Register the control on the page


<@ Register Tagprefix="Nishnat" Namespace="Nishnat.ListTreeView" Assembly="Nishnat.ListTreeView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=57c7737278844f55">


* Now Use it on the page

<Nishnat:ListItems Id="listItems" runat="server" ListName="Books">
</Nishnat:ListItems>
<Nishnat:AllListsWithItems Id="AllListsWithItems" runat="server">
</Nishnat:AllListsWithItems>