Set Current Navigation/Quick Launch and Global Navigation with CSOM

I was recently working on a site and at first glance, couldn’t find any obvious way to manipulate Structural Navigation settings using CSOM.

As it turns out setting the navigation with CSOM is quite different from doing it from server side code or PowerShell which looks like the following (in PowerShell).

$assetsWeb = Get-SPWeb "url"
$SPPubWebAssets = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($assetsWeb)
$SPPubWebAssets.Navigation.InheritCurrent = $false
$SPPubWebAssets.Navigation.ShowSiblings = $false
$SPPubWebAssets.Navigation.CurrentIncludeSubSites = $false
$SPPubWebAssets.Navigation.CurrentIncludePages = $false
$SPPubWebAssets.Update()
$assetsWeb.dispose()

Thankfully, I found this post on technet which discussed the issue I was having and included the following code:

web.AllProperties[“__CurrentNavigationIncludeTypes”] = "3";

The answer worked a charm but I was a little unsure about what “3” was specifying. After a little more digging I found that the values range from 0-3 and are ordered in the way the checkboxes are presented through the user interface (as shown in the screen shot above).

Here are the values for the __GlobalNavigationIncludeTypes and __CurrentNavigationIncludeTypes options

0 = don't show pages or subsites
1 = show subsites only
2 = show pages only
3 = show subsites and pages

Full CSOM code inside a Cmdlet

/// <summary>
    /// Set quick launch navigation settings for web
    /// </summary>
    /// <remarks>
    /// <strong>Arguments<br/></strong>
    /// <strong>Parent (Web):</strong> The parent Web under which to change the settings<br/>
    /// <strong>NavigationSource (String):</strong> Whether the web should use InheritFromParentWeb, PortalProvider, TaxonomyProvider or Unknown for quick launch.<br/>
    /// <strong>CurrentNavigationIncludeTypes (String):</strong> Specify values for including types. 0 = don't show pages or subsites, 1 = show subsites only, 2 = show pages only, 3 = show subsites and pages<br/>
    /// </remarks>
    /// <example><code>Set-SPOQuickLaunchNavigationSettings $parent "PortalProvider" “3”</code></example>
    [Cmdlet(VerbsCommon.Set, "SPOQuickLaunchNavigationSettings")]
    public class SetSPOQuickLaunchNavigationSettings : Cmdlet
    {
        [Parameter(Position = 0), ValidateNotNullOrEmpty]
        public Web Parent { get; set; }

        [Parameter(Position = 1), ValidateNotNullOrEmpty]
        public String NavigationSource { get; set; }

        [Parameter(Position = 2), ValidateNotNullOrEmpty]
        public String NavigationIncludeTypes { get; set; }

        protected override void ProcessRecord()
        {
            var Context = Parent.Context;

            var taxonomySession = TaxonomySession.GetTaxonomySession(Context);
            var settings = new WebNavigationSettings(Context, Parent);

            var navigationSource = StandardNavigationSource.PortalProvider;
            switch (NavigationSource)
            {
                case "PortalProvider" : navigationSource = StandardNavigationSource.PortalProvider; break;
                case "InheritFromParentWeb": navigationSource = StandardNavigationSource.InheritFromParentWeb; break;
                case "TaxonomyProvider": navigationSource = StandardNavigationSource.TaxonomyProvider; break;
                case "Unknown": navigationSource = StandardNavigationSource.Unknown; break;
                default : navigationSource = StandardNavigationSource.PortalProvider; break;
            }
            settings.CurrentNavigation.Source = navigationSource;
            settings.Update(taxonomySession);

            Parent.AllProperties["__CurrentNavigationIncludeTypes"] = NavigationIncludeTypes;

            Parent.Update();
            Context.ExecuteQuery();
        }
    }

 

4 thoughts on “Set Current Navigation/Quick Launch and Global Navigation with CSOM

  1. Chris OConnor

    THANKS !   This was the trick I needed – for turning off the checkboxes on an Office365 SharePoint site.   I had also tried using Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings – but no luck.    Thanks for the post – cheers !

    Reply
      1. Sumit Rampal

        Hi Bex,

        Somehow this same code is not unchecking Show Subsites option for Structural Navigation at the Root Site level. Working absolutely fine at sub sites level though.

        Thanks

        Reply
        1. bexgordon Post author

          That’s really strange! I can’t remember whether I was doing only subsites or root level as well. Hard to know what Microsoft will change over time in the back end too 🙂 if you do find a solution let me know! Cheers

          Reply

Leave a Reply

Your email address will not be published. Required fields are marked *