Thursday, July 2, 2009

Navigation - Custom TreeView control

This SPSTreeViewNavigator, a custom asp.net TreeView control provides simple SharePoint sites navigation feature. It can be bundled either in the web part or used on the custom .aspx page to provide site relative navigational TreeView. This control exposes RootWeb property. Set it to SPWeb object of a site. It will create navigational TreeView representing site hierarchy of associated site. When assigned with RootWeb of site collection, it will create navigational TreeView for entire site collection.

TreeView is populated on demand to achieve better performance.




   1:  using System;
   2:  using System.Web.UI.WebControls;
   3:  using System.Xml;
   4:  using System.Collections;
   5:  using System.Collections.Generic;
   6:  using Microsoft.SharePoint;
   7:  using System.Text.RegularExpressions;
   8:   
   9:  namespace YourNameSpace.Controls
  10:  {
  11:      public class SPSTreeViewNavigator : TreeView
  12:      {
  13:          private SPWeb _rootWeb;
  14:          public string NODE_IMAGE_URL = "/_layouts/Images/cat.gif";
  15:   
  16:          public SPWeb RootWeb
  17:          {
  18:              get { return _rootWeb; }
  19:              set { _rootWeb = value; }
  20:          }
  21:   
  22:          protected override void OnLoad(EventArgs e)
  23:          {
  24:              base.OnLoad(e);
  25:              InitializeTreeView();
  26:          }
  27:   
  28:          //Populate TreeView on demand 
  29:          void DataViewTreeView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
  30:          {
  31:              PopulateChildNodes(e.Node);
  32:          }
  33:   
  34:          void InitializeTreeView()
  35:          {
  36:              this.Height = Unit.Percentage(100);
  37:              this.Width = Unit.Percentage(100);
  38:              this.ShowCheckBoxes = TreeNodeTypes.All;
  39:              this.EnableClientScript = true;
  40:              this.ShowExpandCollapse = true;
  41:              //View State must be enable  
  42:              this.EnableViewState = true;
  43:              this.ShowLines = true;
  44:              this.TreeNodePopulate += DataViewTreeView_TreeNodePopulate;
  45:              this.NodeStyle.ImageUrl = NODE_IMAGE_URL;
  46:              if (!this.Page.IsPostBack)
  47:              {
  48:                  this.Nodes.Clear(); PopulateRootNode(this);
  49:              }
  50:          }
  51:   
  52:          void PopulateChildNodes(TreeNode node)
  53:          {
  54:              Guid webGuidId = new Guid(node.Value);
  55:              using (SPSite spSite = new SPSite(_rootWeb.Site.ID))
  56:              {

  57:                  using (SPWeb web = spSite.OpenWeb(webGuidId))
  58:                  {
  59:                      foreach (SPWeb spWeb in web.Webs)
  60:                      {
  61:                          PopulateTreeNode(spWeb, node);
  62:                      }
  63:                  }
  64:              }
  65:          }
  66:   
  67:          private void PopulateTreeNode(SPWeb web, TreeNode node)
  68:          {
  69:              TreeNode newNode = new TreeNode(web.Title, web.ID.ToString(),
  70:                                     NODE_IMAGE_URL, web.ServerRelativeUrl, string.Empty); newNode.PopulateOnDemand = true;
  71:              newNode.Expanded = false;
  72:              newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
  73:              node.ChildNodes.Add(newNode);
  74:          }
  75:   
  76:          //Populate root node and next level child nodes  
  77:          void PopulateRootNode(TreeView treeView)
  78:          {

  79:              TreeNode node = new TreeNode();
  80:              SPWeb web = _rootWeb; node.Text = web.Title;
  81:              node.Value = web.ID.ToString();
  82:              node.NavigateUrl = web.ServerRelativeUrl;
  83:              node.PopulateOnDemand = true;
  84:              node.ShowCheckBox = true;
  85:              node.Expand();
  86:              treeView.Nodes.Add(node);
  87:          }
  88:      }
  89:  }
  90:   
  91:   
  92:   


Following is example of client code. Create instance of SPSTreeViewNavigator control and initialize it's RootWeb property with SPWeb object.



   1:              SPSTreeViewNavigator treeView = new SPSTreeViewNavigator();

   2:              treeView.RootWeb = SPContext.Current.Site.OpenWeb();

   3:              Controls.Add(treeView);



Following image shows SPSTreeViewNavigator in action, with TreeView fully expanded.
 

1 comment: