WebNavigationSettings and CSOM

WebNavigationSettings and CSOM

Whenever you create a new website based on a custom template you still might end up with the Managed Navigation enabled. I blogged about that a few weeks ago Custom web templates and confusing Managed Navigation regarding a FTC project that I did. In a more recent scenario we encountered the sort of the same problem when deploying to Office 365. We did not have any webtemplates but used the out of the box Publishing Portal template to create some new Site Collections. Once created we created some new lists and updated some default lists to be visible on the Quick Launch.

Web web = clientContext.Web;

web.Lists.Add(new ListCreationInformation {
    Title = "Sample",
    Description = "Sample List",
    Url = "Lists/Sample",
    TemplateType = 100,
    TemplateFeatureId = new Guid("00BFEA71-DE22-43B2-A848-C05709900100"),
    QuickLaunchOption = QuickLaunchOptions.On
});

List someList = web.Lists.GetByTitle("OtherSample");
someList.OnQuickLaunch = true;
someList.Update(); 

clientContext.Load(someList);                    
clientContext.ExecuteQuery();

After creating and modifying these lists when you will check your site they will not be visible on the Quick Launch as it is set to use Managed Navigation. As pointed out a previously on Custom web templates and confusing Managed Navigation it is fairly easy to fix if you can write FTC. And as it turns out when you are writing CSOM it does not differ that much. You can use the Microsoft.SharePoint.Client.Publishing API that contains a WebNavigationSettings class that you can use. The only difference is that when you execute an update you will have to pass a valid Taxonomy Session.

So first you have to retrieve a Taxonomy Session, then all you need to do is to pass that when you are updating the WebNavigationSettings and you’re done. Other options are (like the navigation source) are all available for you to use

TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
clientContext.Load(taxonomySession);
clientContext.ExecuteQuery();

WebNavigationSettings navigationSettings = new WebNavigationSettings(clientContext, clientContext.Web);
navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
navigationSettings.Update(taxonomySession); 
clientContext.Load(navigationSettings);
clientContext.ExecuteQuery();
There are 4 comments for this article
  1. Eric at 10:53

    So, if you choose to allow the default managed navigation, does that mean that there is no way to programmatically or declaratively make a list show on quick launch?

    • Albert-Jan Schot at 11:03

      With out of the box managed navigation enabled lists will not be shown. The only option you have there is to programmatically add the list to the navigation yourself. As far as I know it cannot be down declaratively but with code you could update the navigation term set with an entry for the list you created.

  2. Eric at 11:30

    Interestingly (or frustratingly), it appears that the OnQuickLaunch declarative property is completely ignored regardless of the navigationsettings. Even with Structural navigation turned on, if I activate a feature with a listinstance set for OnQuickLaunch, it still does not appear. Regardless of navigationsettings, once I manually go to the UI in list settings and choose "Show On Quick Launch", it does.

    • Albert-Jan Schot at 11:52

      So the only option you would have is to do something like from code
      SPNavigationNode node = new SPNavigationNode("ListName", "ListUrl");
      web.Navigation.AddToQuickLaunch(node, SPQuickLaunchHeading.Lists);

Leave a Reply