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.
protected TreeViewImageSet _qlFormat;
[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;
protected override void CreateChildControls()
{
base.CreateChildControls();
trViewQL = new TreeView();
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;
string separate = "";
if (child.Url.IndexOf('?') == -1)
{
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)
{
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();
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;
string separate = "";
if (child.Url.IndexOf('?') == -1)
{
separate = "?CNID=";
}
else
{
separate = "&CNID=";
}
trChild.NavigateUrl = child.Url +separate+child.Title;
if (!IsparentOpen)
{
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