Sunday 9 October 2011

How to hide quick launch in a publishing page in sharepoint 2007 site

Hi,
I was asked to provide a way to hide quick launch in publishing pages in sharepoint 2007 sites.
As a very simple solution I modified the master page to not show quick launch but again I was informed about a requirement where the user group want to do this in only some of the pages and not in all pages of the site using that master page. So after working on this for some time I have managed to do this via page UI using content editor webpart and also programmatically. i am shareing both the ways here. An important point to note here is the CSS classes you are using in your master page to display quick navigation. If you are using different CSS classes then you need to use them instead of what I am using here.

In the below section I will explain how to do this with page UI and content editor webpart.

1. The first step will be to identify the CSS classes used by quick launch. Hence open the site in sharepoint designer.
2. Navigate down to the master page you are using. You do not need to check it out or to do any kind of editing. In the split mode just select the quick launch part and this in turn will select the code portion.Note down the CSS classes from here.


3. Now open the page that is using this master page and you want to hide this quick launch.
4. Edit the page and add a content editor webpart. In the source editor add the following script. If you have different CSS classes then you need to use the same for them.

<style>
.leftNav
{
display:none;
}
.leftNav1, .leftNav2, .leftNav3
{
display: none;
}
</style>

Thats it the page is now without quick launch. In the next section I am showing how to do this via object model. I have hard coded the script but as I have already mentioned above you need to change it according to your master page. Hence instead of embedding it within the code you can take this as an input string as well. I have displayed some messages to give an idea of exactly what inputs are needed while running this application.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint.Publishing;
using System.Xml;

namespace HideQuickLaunch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This will hide the quick launch of a page");
            Console.WriteLine("Please enter the full URL of the page");
            String page = Console.ReadLine();
            String webPartName = String.Empty;
            using (SPSite site = new SPSite(page))
            {
                using (SPWeb web = site.OpenWeb())
                {
             
                  String pageRelativeUrl = page.Remove(0, web.Url.Length+1);
                  ContentEditorWebPart contentEditor = new ContentEditorWebPart();
                  contentEditor.Title = String.Empty;
                  PublishingWeb webPublish = PublishingWeb.GetPublishingWeb(web);
                  PublishingPage pagePublish = webPublish.GetPublishingPages()[pageRelativeUrl];
                  pagePublish.CheckOut();
                  SPWebPartCollection webPartCollection = web.GetWebPartCollection(page, Storage.Shared);
                  webPartCollection.Add(contentEditor);
                  webPartCollection.SaveChanges(contentEditor.StorageKey);
                  ContentEditorWebPart webPartToBeEdited = (ContentEditorWebPart) webPartCollection[contentEditor.StorageKey];
                  XmlDocument xmlDoc = new XmlDocument();
                  XmlElement elementContent = xmlDoc.CreateElement("script");
                  elementContent.InnerText = "<style>\n.leftNav\n{\ndisplay:none;\n}\n.leftNav1, .leftNav2, .leftNav3\n{\ndisplay: none;\n}\n</style>";
                  webPartToBeEdited.Content = elementContent;
                  webPartToBeEdited.AllowHide = true;
                  webPartToBeEdited.Hidden = true;
                  webPartCollection.SaveChanges(webPartToBeEdited.StorageKey);
                  pagePublish.CheckIn("Approved");
                  web.Update();
                  Console.WriteLine("The quick launch is hidden now");
                  Console.ReadLine();
                }
            }
        }
    }
}

I hope this will help you out.

Thanks,
Rahul Rashu


No comments:

Post a Comment