Sunday, December 1, 2013

Adding Left Navigation for SharePoint Publishing site programmatically

Problem Background

I was working on creating a webtemplate based in SharePoint 2013 based on publishing site. There I had an requirement to create Left Navigation programmatic.

Solution Background

Here I have used "Structural Navigation" as the navigation type.


Below is the method to achieve it.

using (SPSite spSite = new SPSite("http://cd-sjamaldeen:23855/sites/002/"))
    using (SPWeb spWeb = spSite.OpenWeb())

        //to remove pages from quick launch navigation
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(spWeb);
        // Current navigation 
        publishingWeb.Navigation.CurrentIncludePages = false;
        publishingWeb.Navigation.CurrentIncludeSubSites = false;
        publishingWeb.Navigation.GlobalIncludePages = false;
        publishingWeb.Navigation.GlobalIncludeSubSites = false;

        var webNavSettings = new WebNavigationSettings(spWeb);

        // Set the current navigation to Structural Navigation type
        webNavSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;

        // Set the global navigation to Structural Navigation type
        webNavSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;


        //add the settings page to the quick launch
        SPNavigationNodeCollection nodeColl = spWeb.Navigation.QuickLaunch;

        //Delete all the navigation nodes from QuickLaunch 
        for (int i = spWeb.Navigation.QuickLaunch.Count - 1; i > -1; i--)


        SPNavigationNode googleNode = new SPNavigationNode("Google", "");

        SPNavigationNode yahooNode = new SPNavigationNode("Yahoo", "");

        spSite.AllowUnsafeUpdates = true;
        spWeb.AllowUnsafeUpdates = true;
        spWeb.AllowUnsafeUpdates = false;
        spSite.AllowUnsafeUpdates = false;



Hope your publishing site now will have the Navigation.
If you face any problems go to site setting ->  Navigation (Under Look and Feel section) see weather all your navigation setting exists.

No comments:

Post a Comment