by Andreas

Sharepoint 2007 / 2010 – display quick launch based on user or role

If you search the web for “hide quick launch menu in Sharepoint”, you’ll see that the common solution is adding a content editor web part, switch to HTML view and add the following:

<style>
   .ms-navframe
   {
       display:none;
   }
</style>

And it works great! Not a surprise, really – after all Sharepoint is an ASP.NET web application no matter how you twist and turn it.

But what if you want the quick launch menu to show and hide depending on roles? I read few interesting approaches there (mostly written by non-developers, and if you are stuck with Sharepoint Designer you are pretty limited I guess..), but I thought a dead simple web part would be a better idea.

This version uses the login (domain + username), but can easily be changed to apply to roles instead. I wont go through the entire code (if anyone is interested in the source code or the compiled package just ask for it and we’ll upload it), and there are several ways to improve this, add more error handling blah blah blah.

Note: let me know if this doesn’t compile (the code was written for Sharepoint 2007). I made some slight changes when posting it here and even though it compiled fine in my head it doesn’t mean your compiler agrees.

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.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Xml;
using System.Collections.Generic;

namespace RoleBasedQuickLaunchHider
{
    [Guid("02b117a3-a30e-4149-8e5e-289076eb5a06")]
    public class RoleBasedQuickLaunchHider : System.Web.UI.WebControls.WebParts.WebPart
    {
        private ContentEditorWebPart _ceWP;

        private string _qlHidden = "<style> .ms-quicklaunch { display:none; } .ms-navframe {  display: none; } </style>";

        private string _AdminUsers;
        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        FriendlyNameAttribute("Administrators"),
        SPWebCategoryName("Degree")]
        public string AdminUsers
        {
            get
            {
                return _AdminUsers;
            }
            set
            {
                _AdminUsers = value;
            }
        }

        private bool _IgnoreFilter;
        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        FriendlyNameAttribute("Activate"),
        SPWebCategoryName("Degree")]
        public bool IgnoreFilter
        {
            get
            {
                return _IgnoreFilter;
            }
            set
            {
                _IgnoreFilter = value;
            }
        }

        public RoleBasedQuickLaunchHider()
        {
            _ceWP = new ContentEditorWebPart();
            _ceWP.Title = "Hide quick launch";
            _ceWP.ChromeType = PartChromeType.None;
            _ceWP.ChromeState = PartChromeState.Normal;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (!IgnoreFilter)
                return; // web part is disabled

            // create hide script that will be added if applicable
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement xmlElementDetails = xmlDoc.CreateElement("DegreeCEWP");
            xmlElementDetails.InnerText = _qlHidden;

            if (string.IsNullOrEmpty(_AdminUsers))
            {
                // no exceptions defined, hide quick launch
                _ceWP.Content = xmlElementDetails;
                base.Controls.Add(_ceWP);
                return;
            }

            // determine if current user is allowed to see quick launch
            string[] adminString = _AdminUsers.Split(';');
            List<string> adminRoles = new List<string>();
            foreach (string str in adminString)
            {
                if (!string.IsNullOrEmpty(str.Trim()))
                    adminRoles.Add(str.Trim());
            }

            if (adminRoles.Contains(SPContext.Current.Web.CurrentUser.LoginName))
                return;

            // user is not defined as admin, quick launch should be hidden, add script
            _ceWP.Content = xmlElementDetails;

            base.Controls.Add(_ceWP);
        }
    }
}
  • Sarvn

    Hi,
    Thanks for your article its a really gud. If pssble iam requesting you please send the full code to the follwoing mail id ie sarvn.g@gmail.com its very ciritical in our project.

    Regards,
    Sarvn