/// <summary> /// Updates navigation settings for the current web /// </summary> /// <param name="web"></param> /// <param name="navigationSettings"></param> public static void UpdateNavigationSettings(this Web web, AreaNavigationEntity navigationSettings) { //Read all the properties of the web web.Context.Load(web, w => w.AllProperties); web.Context.ExecuteQueryRetry(); if (!ArePublishingFeaturesActivated(web.AllProperties)) { throw new ArgumentException("Structural navigation settings are only supported for publishing sites"); } // Use publishing CSOM API to switch between managed metadata and structural navigation TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(web.Context); web.Context.Load(taxonomySession); web.Context.ExecuteQueryRetry(); Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings webNav = new Publishing.Navigation.WebNavigationSettings(web.Context, web); if (!navigationSettings.GlobalNavigation.ManagedNavigation) { webNav.GlobalNavigation.Source = Publishing.Navigation.StandardNavigationSource.PortalProvider; } else { webNav.GlobalNavigation.Source = Publishing.Navigation.StandardNavigationSource.TaxonomyProvider; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { webNav.CurrentNavigation.Source = Publishing.Navigation.StandardNavigationSource.PortalProvider; } else { webNav.CurrentNavigation.Source = Publishing.Navigation.StandardNavigationSource.TaxonomyProvider; } webNav.Update(taxonomySession); web.Context.ExecuteQueryRetry(); //Read all the properties of the web again after the above update web.Context.Load(web, w => w.AllProperties); web.Context.ExecuteQueryRetry(); if (!navigationSettings.GlobalNavigation.ManagedNavigation) { int globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation); web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType; web.AllProperties[GlobalDynamicChildLimit] = navigationSettings.GlobalNavigation.MaxDynamicItems; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { int currentNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.CurrentNavigation); web.AllProperties[CurrentNavigationIncludeTypes] = currentNavigationIncludeType; web.AllProperties[CurrentDynamicChildLimit] = navigationSettings.CurrentNavigation.MaxDynamicItems; // Call web.update before the IsSubSite call as this might do an ExecuteQuery. Without the update called the changes will be lost web.Update(); // For the current navigation there's an option to show the sites siblings in structural navigation if (web.IsSubSite()) { web.AllProperties[NavigationShowSiblings] = navigationSettings.CurrentNavigation.ShowSiblings.ToString(); } } // if there's either global or current structural navigation then update the sorting settings if (!navigationSettings.GlobalNavigation.ManagedNavigation || !navigationSettings.CurrentNavigation.ManagedNavigation) { // If there's automatic sorting or pages are shown with automatic page sorting then we can set all sort options if ((navigationSettings.Sorting == StructuralNavigationSorting.Automatically) || (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically && (navigationSettings.GlobalNavigation.ShowPages || navigationSettings.CurrentNavigation.ShowPages))) { // All sort options can be set web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting; web.AllProperties[NavigationAutomaticSortingMethod] = (int)navigationSettings.SortBy; web.AllProperties[NavigationSortAscending] = navigationSettings.SortAscending.ToString(); } else { // if pages are not shown we can set sorting to either automatic or manual if (!navigationSettings.GlobalNavigation.ShowPages && !navigationSettings.CurrentNavigation.ShowPages) { if (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically) { throw new ArgumentException("Sorting can only be set to StructuralNavigationSorting.ManuallyButPagesAutomatically when ShowPages has been selected in either the global or current structural navigation settings"); } } web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting; } } //Persist all property updates at once web.Update(); web.Context.ExecuteQueryRetry(); }
/// <summary> /// Updates navigation settings for the current web /// </summary> /// <param name="web"></param> /// <param name="navigationSettings"></param> public static void UpdateNavigationSettings(this Web web, AreaNavigationEntity navigationSettings) { //Read all the properties of the web web.Context.Load(web, w => w.AllProperties); web.Context.ExecuteQueryRetry(); if (!ArePublishingFeaturesActivated(web.AllProperties)) { throw new ArgumentException("Structural navigation settings are only supported for publishing sites"); } // Use publishing CSOM API to switch between managed metadata and structural navigation TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(web.Context); web.Context.Load(taxonomySession); web.Context.ExecuteQueryRetry(); Microsoft.SharePoint.Client.Publishing.Navigation.WebNavigationSettings webNav = new Publishing.Navigation.WebNavigationSettings(web.Context, web); if (!navigationSettings.GlobalNavigation.ManagedNavigation) { webNav.GlobalNavigation.Source = Publishing.Navigation.StandardNavigationSource.PortalProvider; } else { webNav.GlobalNavigation.Source = Publishing.Navigation.StandardNavigationSource.TaxonomyProvider; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { webNav.CurrentNavigation.Source = Publishing.Navigation.StandardNavigationSource.PortalProvider; } else { webNav.CurrentNavigation.Source = Publishing.Navigation.StandardNavigationSource.TaxonomyProvider; } webNav.Update(taxonomySession); web.Context.ExecuteQueryRetry(); //Read all the properties of the web again after the above update web.Context.Load(web, w => w.AllProperties); web.Context.ExecuteQueryRetry(); if (!navigationSettings.GlobalNavigation.ManagedNavigation) { int globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation); web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType; web.AllProperties[GlobalDynamicChildLimit] = navigationSettings.GlobalNavigation.MaxDynamicItems; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { int currentNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.CurrentNavigation); web.AllProperties[CurrentNavigationIncludeTypes] = currentNavigationIncludeType; web.AllProperties[CurrentDynamicChildLimit] = navigationSettings.CurrentNavigation.MaxDynamicItems; // Call web.update before the IsSubSite call as this might do an ExecuteQuery. Without the update called the changes will be lost web.Update(); // For the current navigation there's an option to show the sites siblings in structural navigation if (web.IsSubSite()) { web.AllProperties[NavigationShowSiblings] = navigationSettings.CurrentNavigation.ShowSiblings.ToString(); } } // if there's either global or current structural navigation then update the sorting settings if (!navigationSettings.GlobalNavigation.ManagedNavigation || !navigationSettings.CurrentNavigation.ManagedNavigation) { // If there's automatic sorting or pages are shown with automatic page sorting then we can set all sort options if ((navigationSettings.Sorting == StructuralNavigationSorting.Automatically) || (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically && (navigationSettings.GlobalNavigation.ShowPages || navigationSettings.CurrentNavigation.ShowPages))) { // All sort options can be set web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting; web.AllProperties[NavigationAutomaticSortingMethod] = (int)navigationSettings.SortBy; web.AllProperties[NavigationSortAscending] = navigationSettings.SortAscending.ToString(); } else { // if pages are not shown we can set sorting to either automatic or manual if (!navigationSettings.GlobalNavigation.ShowPages && !navigationSettings.CurrentNavigation.ShowPages) { if (navigationSettings.Sorting == StructuralNavigationSorting.ManuallyButPagesAutomatically) { throw new ArgumentException("Sorting can only be set to StructuralNavigationSorting.ManuallyButPagesAutomatically when ShowPages has been selected in either the global or current structural navigation settings"); } } web.AllProperties[NavigationOrderingMethod] = (int)navigationSettings.Sorting; } } //Persist all property updates at once web.Update(); web.Context.ExecuteQueryRetry(); }