Click here to Skip to main content
15,888,293 members
Articles / Productivity Apps and Services / Sharepoint

Customize QuickLaunch Webpart

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Dec 2009CPOL1 min read 31.9K   285   8  
Sharepoint quicklaunch webpart
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Specialized;
using System.Collections;
using Microsoft.SharePoint.Utilities;
using System.ComponentModel;

namespace CustomizeQuickLaunch
{
    [Guid("e5fce1f6-1b7c-4ff6-9941-c673deb1108c")]
    public class QuickLaunch : System.Web.UI.WebControls.WebParts.WebPart
    {
        public QuickLaunch()
        {
        }
        
        // A treeview control image set enum variable
        protected TreeViewImageSet _qlFormat;

        
        //property use to change the image set of tree view control
        // Creates a custom property that is an enum.
        // This property will be displayed as a drop-down list in the
        // property pane.
        [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; }
        }


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

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

        // method use to build quick launch in treeview control. I put them in OnPreRender methods ?
        // answers is by putting them here it reflects the changes every time webpart property gets change.
        protected override void OnPreRender(EventArgs e)
        {
	    // for mentaining 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);
	                }
	            }
	        
        }

	// render the control
        protected override void Render(HtmlTextWriter writer)
        {
            trViewQL.RenderControl(writer);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Sidat Hyder Morshed Associates Pvt Ltd
Pakistan Pakistan
MCP,MCTS

Comments and Discussions