Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

Customize QuickLaunch Webpart

0.00/5 (No votes)
16 Dec 2009CPOL1 min read 32.5K   285  
Sharepoint quicklaunch webpart
CustomizeQuickLaunch

Introduction

Sharepoint 2007 is an awesome solution for web portal, and almost did a great job to gather the enterprises attention. Sharepoint object model gives great power to the developer to customize Sharepoint according to their requirements. Here customize Sharepoint quicklaunch as you see it above.

Background

QuickLaunch in Sharepoint is used for fast naviagtion to lists, document libraries, sites, etc. for users. Problem is more sites and lists added to bigger the quicklaunch, to customize quick launch to be collapsible. We use Sharepoint object model here to build quick launch webpart.

Using the Code

Create a webpart using Sharepoint Visual Studio template

Initialize tree view image set property to change the image set by changing the webpart property.This property will be displayed as a drop-down list in the property pane.

C#
// A treeview control image set enum variable
protected TreeViewImageSet _qlFormat;  
C#
/// property use to change the image set of tree view control
[Personalizable(PersonalizationScope.Shared), 
WebBrowsable, WebDisplayName("Format for quick launch"),
WebDescription("")]
[DefaultValue(TreeViewImageSet.Arrows)]
[WebPartStorage(Storage.Personal)]
[FriendlyName("Format List")]
[Description("Select a value from the dropdown list.")]
[Browsable(true)]
public TreeViewImageSet SelectFormate
{
    set { _qlFormat = value; }
    get { return _qlFormat; }
} 

Add control to webpart:

C#
TreeView trViewQL; // tree view control variable
protected override void CreateChildControls()
{
    base.CreateChildControls();

    trViewQL = new TreeView();
    // add control to webpart
    this.Controls.Add(trViewQL);
}  

We need to maintain the treeview state. So we attach the querystring to each URL of the node.

C#
TreeNode trChild = new TreeNode();
trChild.Text = child.Title;
// set the url for maintaining the state of the control
string separate = "";
if (child.Url.IndexOf('?') == -1) // checking if URL has already 
                                  // some query string variable attached
{
     separate = "?CNID=";
}
else
{
     separate = "&CNID=";
}
trChild.NavigateUrl = child.Url +separate+child.Title; 

The method is used to build quick launch in treeview control. I put them in OnPreRender methods? The answer is by putting them here, it reflects the changes every time webpart property changes.

C#
protected override void OnPreRender(EventArgs e)
    {
	// for maintaining the state of the control. 
         string currentId = "";
         if (System.Web.HttpContext.Current.Request.QueryString.Get("CNID") != null)
         {
             currentId = 
	     System.Web.HttpContext.Current.Request.QueryString.Get("CNID").ToString();
         }
         trViewQL.Nodes.Clear();
	/// apply the image set in the property
         trViewQL.ImageSet = SelectFormate;
         base.OnPreRender(e);
         trViewQL.CollapseAll();

         SPWeb web = SPContext.Current.Web;
	        
	         SPNavigationNodeCollection qlNodes = web.Navigation.QuickLaunch;
             foreach (SPNavigationNode node in qlNodes)
	         {
	             if (node.IsExternal)
                  {
                     bool IsparentOpen = false;
                     TreeNode trParent = new TreeNode();
                     trParent.Text = node.Title;
                     trParent.NavigateUrl = node.Url;

                     SPNavigationNodeCollection children = node.Children;
	                   foreach (SPNavigationNode child in children)
                     {
                         TreeNode trChild = new TreeNode();
                         trChild.Text = child.Title;
	                // set the URL for maintaining the state of the control
                         string separate = "";
                         if (child.Url.IndexOf('?') == -1)
                         {
                             separate = "?CNID=";
                         }
                         else
                         {
                             separate = "&CNID=";
                         }
                         trChild.NavigateUrl = child.Url +separate+child.Title;
	
                         if (!IsparentOpen)
                         {
			  // expand the clicked node and collapse others
                             if (currentId.Equals(child.Title))
                             {
                                 trParent.Expanded = true;
                                 trChild.Selected = true;
                                 IsparentOpen = true;
                             }
                             else
                             {
                                 trParent.Expanded = false;
                             }
                          }
                          trParent.ChildNodes.Add(trChild);
	                 }
                     trViewQL.Nodes.Add(trParent);
	            }
	         }
     }

Finally render the control by overriding Render method:

C#
protected override void Render(HtmlTextWriter writer)
{
     trViewQL.RenderControl(writer);
}

Build and Deploy the Project

Use the Visual Studio Build and deploy your project.

History

  • 17th December, 2009: Initial post

License

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