Customize QuickLaunch Webpart





0/5 (0 vote)
Sharepoint quicklaunch webpart

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.
// A treeview control image set enum variable
protected TreeViewImageSet _qlFormat;
/// 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:
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.
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.
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:
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