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();