How to enable structural navigation for the quick launch navigation in CSOM?
-
12-12-2020 - |
Pregunta
I create programmatically in CSOM/PNP a new subsite. This subsite do not needs to enhirit the quick launch navigation of the parent. I would like to set the navigation to structural navigation. And do not want to show subsites and pages. How to do that?
I did it already in powershell but cannot convert it to CSOM. Here is the powershell code:
$web = get-spweb 'http://mycompany.com'
# enable structural navigation for the global/quick launch navigation
$navSetting = new-object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings($web)
$navSetting.CurrentNavigation.Source=[Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::PortalProvider
$navSetting.Update()
# disable including pages/subsites for the quick launch navigation
$SPPubWeb.Navigation.CurrentIncludeSubSites = $false
$SPPubWeb.Navigation.CurrentIncludePages = $false
$SPPubWeb.Update()
$web.Dispose()
Solución 2
I found a solution with less code which I think it is the best. It is using CSOM Office Dev PnP:
AreaNavigationEntity nav = new AreaNavigationEntity();
nav.CurrentNavigation.ShowSubsites = false;
nav.CurrentNavigation.ShowPages = false;
web.UpdateNavigationSettings(nav);
Otros consejos
For enabling structural navigation you can use the code below:
var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
var settings = new WebNavigationSettings(clientContext, web);
settings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
settings.Update(taxonomySession);
clientContext.ExecuteQuery();
For other settings consider using this excellent helper class by Vadim Gremyachev:
var navigation = new ClientPortalNavigation(ctx.Web);
navigation.CurrentIncludeSubSites = false;
navigation.CurrentIncludePages = false;
navigation.SaveChanges();
I found also this solution, it is almost the same as the solution of Kai just without the extra ClientPortalNavigation.cs file:
// enable structural navigation for the quick launch navigation
TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(newWeb.Context);
newWeb.Context.Load(taxonomySession);
newWeb.Context.ExecuteQuery();
WebNavigationSettings navigationSettings = new WebNavigationSettings(newWeb.Context, newWeb);
navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
navigationSettings.Update(taxonomySession);
newWeb.Context.Load(navigationSettings);
newWeb.Context.ExecuteQuery();
// disable quick launch navigation including subsites
string IncludeSubsitesKey = "__IncludeSubSitesInNavigation";
if (newWeb.GetPropertyBagValueString(IncludeSubsitesKey, string.Empty) != null)
newWeb.RemovePropertyBagValue(IncludeSubsitesKey);
newWeb.SetPropertyBagValue(IncludeSubsitesKey, bool.FalseString);