/// <summary> /// Sets the web navigation settings. /// </summary> /// <param name="web">The web.</param> /// <param name="settings">The settings.</param> public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings) { var taxonomySession = new TaxonomySession(web.Site); if (taxonomySession.TermStores.Count > 0) { var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession); var group = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site); var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label); // Flag the term set as a navigation term set termSet.SetCustomProperty(SystemIsNavigationTermSet, "True"); termStore.CommitAll(); var navigationSettings = new WebNavigationSettings(web); navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.GlobalNavigation.TermStoreId = termStore.Id; navigationSettings.GlobalNavigation.TermSetId = termSet.Id; navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.CurrentNavigation.TermStoreId = termStore.Id; navigationSettings.CurrentNavigation.TermSetId = termSet.Id; navigationSettings.AddNewPagesToNavigation = settings.AddNewPagesToNavigation; navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages; navigationSettings.Update(taxonomySession); if (settings.PreserveTaggingOnTermSet) { termSet.IsAvailableForTagging = true; termStore.CommitAll(); } } }
/// <summary> /// Reset web navigation to its default configuration. Disabled the term set as navigation term set. /// </summary> /// <param name="web">The web</param> /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param> public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings) { var taxonomySession = new TaxonomySession(web.Site); if (taxonomySession.TermStores.Count > 0) { if (settings != null) { // Disable the navigation flag on the the term set var termStore = taxonomySession.DefaultSiteCollectionTermStore; var group = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name); var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label); const string PropertyName = "_Sys_Nav_IsNavigationTermSet"; string propertyValue; if (termSet.CustomProperties.TryGetValue(PropertyName, out propertyValue)) { termSet.DeleteCustomProperty(PropertyName); termSet.TermStore.CommitAll(); } } var navigationSettings = new WebNavigationSettings(web); navigationSettings.ResetToDefaults(); navigationSettings.Update(taxonomySession); } }
/// <summary> /// Reset web navigation to its default configuration. Disabled the term set as navigation term set. /// </summary> /// <param name="web">The web</param> /// <param name="settings">The managed navigation settings. Set null if you want to keep the associated termset unchanged</param> public void ResetWebNavigationToDefault(SPWeb web, ManagedNavigationInfo settings) { var taxonomySession = new TaxonomySession(web.Site); if (taxonomySession.TermStores.Count > 0) { if (settings != null) { // Disable the navigation flag on the the term set var termStore = settings.TermSet.ResolveParentTermStore(taxonomySession); var group = settings.TermSet.ResolveParentGroup(taxonomySession, web.Site); var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label); string propertyValue; if (termSet.CustomProperties.TryGetValue(SystemIsNavigationTermSet, out propertyValue)) { termSet.DeleteCustomProperty(SystemIsNavigationTermSet); termStore.CommitAll(); } } var navigationSettings = new WebNavigationSettings(web); navigationSettings.ResetToDefaults(); navigationSettings.Update(taxonomySession); } }
// Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data. public void ChangeNavigationSettings(ClientContext cc, StandardNavigationSource gSource, StandardNavigationSource cSource) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc); taxonomySession.UpdateCache(); cc.Load(taxonomySession, ts => ts.TermStores); cc.ExecuteQueryRetry(); var navigationSettings = new WebNavigationSettings(cc, cc.Web); navigationSettings.GlobalNavigation.Source = gSource; navigationSettings.CurrentNavigation.Source = cSource; navigationSettings.Update(taxonomySession); try { cc.ExecuteQueryRetry(); } catch (Exception) // if termset not found then set newly created termset to managed navigation { TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); Microsoft.SharePoint.Client.Taxonomy.TermGroup group = termStore.GetTermGroupByName("TG_1"); // TG_1 is a term group mentioned in navigation_add_1605.xml Microsoft.SharePoint.Client.Taxonomy.TermSet termset = group.TermSets.GetByName("TS_1_1"); // TS_1_1 is a term set mentioned in navigation_add_1605.xml cc.Load(termStore); cc.Load(group, g => g.TermSets); cc.Load(termset); cc.ExecuteQuery(); if (StandardNavigationSource.TaxonomyProvider == gSource) { navigationSettings.GlobalNavigation.TermStoreId = termStore.Id; navigationSettings.GlobalNavigation.TermSetId = termset.Id; } if (StandardNavigationSource.TaxonomyProvider == cSource) { navigationSettings.CurrentNavigation.TermStoreId = termStore.Id; navigationSettings.CurrentNavigation.TermSetId = termset.Id; } navigationSettings.GlobalNavigation.Source = gSource; navigationSettings.CurrentNavigation.Source = cSource; navigationSettings.Update(taxonomySession); cc.ExecuteQueryRetry(); } }
static void SetManagedNavigation(SPSite site, string groupName, string termSetName) { TaxonomySession session = new TaxonomySession(site); TermStore termStore = session.TermStores["Managed Metadata Service"]; Group group = termStore.Groups[groupName]; TermSet termSet = group.TermSets[termSetName]; WebNavigationSettings settings = new WebNavigationSettings(site.RootWeb); settings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; settings.GlobalNavigation.TermStoreId = termStore.Id; settings.GlobalNavigation.TermSetId = termSet.Id; settings.Update(); }
public void ConfigureTaxonomyNavigation() { using (SPSite site = new SPSite(TestConfig.ServerUrl)) { using (SPWeb web = site.OpenWeb()) { TaxonomySession taxonomySession = new TaxonomySession(site, updateCache: true); NavigationTermSet termSet = DemoUtilities.RecreateSampleNavTermSet( this.TestContext, taxonomySession, web); // Clear any old settings. WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web); webNavigationSettings.ResetToDefaults(); webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(site); this.WaitForSync(); // Verify the TermSet object is not running. NavigationTermSet actualTermSet; actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider, includeInheritedSettings: true); Assert.IsTrue(actualTermSet == null); // Assign the new settings. webNavigationSettings = new WebNavigationSettings(web); // GlobalNavigation = top menu (aka "top nav") // CurrentNavigation = left menu (aka "quick launch") webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id; webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(site); this.WaitForSync(); actualTermSet = TaxonomyNavigation.GetTermSetForWeb(web, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider, includeInheritedSettings: true); Assert.AreEqual(termSet.Id, actualTermSet.Id); } } }
/// <summary> /// Sets the current navigation asynchronous. /// </summary> /// <param name="web">The web.</param> /// <param name="source">The source.</param> /// <param name="displaySettings">The display settings.</param> public async Task SetCurrentNavigationAsync(Web web, StandardNavigationSource source, NavigationType displaySettings) { var targetWeb = web ?? _web; var ctx = targetWeb.Context; var settings = new WebNavigationSettings(ctx, targetWeb); settings.CurrentNavigation.Source = source; settings.Update(TaxonomySession.GetTaxonomySession(ctx)); ctx.Load(targetWeb, w => w.AllProperties); await ctx.ExecuteQueryAsync(); targetWeb.AllProperties["__CurrentNavigationIncludeTypes"] = ((int)displaySettings).ToString(); targetWeb.AllProperties["__NavigationShowSiblings"] = bool.TrueString; targetWeb.Update(); ctx.Load(targetWeb); await ctx.ExecuteQueryAsync(); }
//Added for Feature 152 public void SetManagedNavigationOn(Guid termSetID) { TaxonomySession ts = TaxonomySession.GetTaxonomySession(_ctx); var tsc = ts.TermStores; _ctx.Load(tsc); _ctx.ExecuteQuery(); WebNavigationSettings navSettings = new WebNavigationSettings(_ctx, _ctx.Web); navSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; navSettings.GlobalNavigation.TermStoreId = tsc[0].Id; navSettings.GlobalNavigation.TermSetId = termSetID; navSettings.Update(ts); _ctx.Load(navSettings); _ctx.ExecuteQuery(); }
/// Configures the web to use Taxonomy Navigation with the sample term set. public static NavigationTermSet SetUpSampleNavTermSet(TestContext testContext, TaxonomySession taxonomySession, SPWeb web) { NavigationTermSet termSet = RecreateSampleNavTermSet(testContext, taxonomySession, web); // Clear any old settings. WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web); webNavigationSettings.ResetToDefaults(); webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id; webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.CurrentNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.CurrentNavigation.TermSetId = termSet.Id; webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(web.Site); return termSet; }
/// Configures the web to use Taxonomy Navigation with the sample term set. public static NavigationTermSet SetUpSampleNavTermSet(TestContext testContext, TaxonomySession taxonomySession, SPWeb web) { NavigationTermSet termSet = RecreateSampleNavTermSet(testContext, taxonomySession, web); // Clear any old settings. WebNavigationSettings webNavigationSettings = new WebNavigationSettings(web); webNavigationSettings.ResetToDefaults(); webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id; webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.CurrentNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.CurrentNavigation.TermSetId = termSet.Id; webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(web.Site); return(termSet); }
/// <summary> /// Sets the web navigation settings. /// </summary> /// <param name="web">The web.</param> /// <param name="settings">The settings.</param> public void SetWebNavigationSettings(SPWeb web, ManagedNavigationInfo settings) { var taxonomySession = new TaxonomySession(web.Site); if (taxonomySession.TermStores.Count > 0) { // we assume we're always dealing with the site coll's default term store var termStore = taxonomySession.DefaultSiteCollectionTermStore; var group = this.taxonomyService.GetTermGroupFromStore(termStore, settings.TermGroup.Name); var termSet = this.taxonomyService.GetTermSetFromGroup(termStore, group, settings.TermSet.Label); // Flag the term set as a navigation term set termSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True"); termSet.TermStore.CommitAll(); var navigationSettings = new WebNavigationSettings(web); navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.GlobalNavigation.TermStoreId = termStore.Id; navigationSettings.GlobalNavigation.TermSetId = termSet.Id; navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.CurrentNavigation.TermStoreId = termStore.Id; navigationSettings.CurrentNavigation.TermSetId = termSet.Id; navigationSettings.AddNewPagesToNavigation = settings.AddNewPagesToNavigation; navigationSettings.CreateFriendlyUrlsForNewPages = settings.CreateFriendlyUrlsForNewsPages; navigationSettings.Update(taxonomySession); if (settings.PreserveTaggingOnTermSet) { termSet.IsAvailableForTagging = true; termSet.TermStore.CommitAll(); } } }
/// <summary> /// Updates navigation settings for the current web /// </summary> /// <param name="web">Web to process</param> /// <param name="navigationSettings">Navigation settings to update</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 var taxonomySession = TaxonomySession.GetTaxonomySession(web.Context); web.Context.Load(taxonomySession); web.Context.ExecuteQueryRetry(); var webNav = new WebNavigationSettings(web.Context, web); if (navigationSettings.GlobalNavigation.InheritFromParentWeb) { if (web.IsSubSite()) { webNav.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb; } else { throw new ArgumentException("Cannot inherit global navigation on root site."); } } else if (!navigationSettings.GlobalNavigation.ManagedNavigation) { webNav.GlobalNavigation.Source = StandardNavigationSource.PortalProvider; } else { webNav.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; } if (navigationSettings.CurrentNavigation.InheritFromParentWeb) { if (web.IsSubSite()) { webNav.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb; } else { throw new ArgumentException("Cannot inherit current navigation on root site."); } } else if (!navigationSettings.CurrentNavigation.ManagedNavigation) { webNav.CurrentNavigation.Source = StandardNavigationSource.PortalProvider; } else { webNav.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; } // If managed metadata navigation is used, set settings related to page creation if (navigationSettings.GlobalNavigation.ManagedNavigation || navigationSettings.CurrentNavigation.ManagedNavigation) { webNav.AddNewPagesToNavigation = navigationSettings.AddNewPagesToNavigation; webNav.CreateFriendlyUrlsForNewPages = navigationSettings.CreateFriendlyUrlsForNewPages; } 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) { var globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation); web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType; web.AllProperties[GlobalDynamicChildLimit] = navigationSettings.GlobalNavigation.MaxDynamicItems; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { var 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(); }
public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation) { using (var scope = new PnPMonitoredScope(this.Name)) { if (template.Navigation != null) { if (!WebSupportsProvisionNavigation(web, template)) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Navigation_Context_web_is_not_publishing); return(parser); } // Check if this is not a noscript site as navigation features are not supported bool isNoScriptSite = web.IsNoScriptSite(); // Retrieve the current web navigation settings var navigationSettings = new WebNavigationSettings(web.Context, web); web.Context.Load(navigationSettings, ns => ns.CurrentNavigation, ns => ns.GlobalNavigation); web.Context.ExecuteQueryRetry(); navigationSettings.AddNewPagesToNavigation = template.Navigation.AddNewPagesToNavigation; navigationSettings.CreateFriendlyUrlsForNewPages = template.Navigation.CreateFriendlyUrlsForNewPages; if (template.Navigation.GlobalNavigation != null) { switch (template.Navigation.GlobalNavigation.NavigationType) { case GlobalNavigationType.Inherit: navigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb; web.Navigation.UseShared = true; break; case GlobalNavigationType.Managed: if (template.Navigation.GlobalNavigation.ManagedNavigation == null) { throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_managed_navigation); } navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.GlobalNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermStoreId)); navigationSettings.GlobalNavigation.TermSetId = Guid.Parse(parser.ParseString(template.Navigation.GlobalNavigation.ManagedNavigation.TermSetId)); break; case GlobalNavigationType.Structural: default: if (template.Navigation.GlobalNavigation.StructuralNavigation == null) { throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_global_structural_navigation); } navigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider; ProvisionGlobalStructuralNavigation(web, template.Navigation.GlobalNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope); break; } if (!isNoScriptSite) { navigationSettings.Update(TaxonomySession.GetTaxonomySession(web.Context)); web.Context.ExecuteQueryRetry(); } } if (template.Navigation.CurrentNavigation != null) { switch (template.Navigation.CurrentNavigation.NavigationType) { case CurrentNavigationType.Inherit: navigationSettings.CurrentNavigation.Source = StandardNavigationSource.InheritFromParentWeb; break; case CurrentNavigationType.Managed: if (template.Navigation.CurrentNavigation.ManagedNavigation == null) { throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_managed_navigation); } navigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.CurrentNavigation.TermStoreId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermStoreId)); navigationSettings.CurrentNavigation.TermSetId = Guid.Parse(parser.ParseString(template.Navigation.CurrentNavigation.ManagedNavigation.TermSetId)); break; case CurrentNavigationType.StructuralLocal: if (!isNoScriptSite) { web.SetPropertyBagValue(NavigationShowSiblings, "false"); } if (template.Navigation.CurrentNavigation.StructuralNavigation == null) { throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation); } navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider; ProvisionCurrentStructuralNavigation(web, template.Navigation.CurrentNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope); break; case CurrentNavigationType.Structural: default: if (!isNoScriptSite) { web.SetPropertyBagValue(NavigationShowSiblings, "true"); } if (template.Navigation.CurrentNavigation.StructuralNavigation == null) { throw new ApplicationException(CoreResources.Provisioning_ObjectHandlers_Navigation_missing_current_structural_navigation); } navigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider; ProvisionCurrentStructuralNavigation(web, template.Navigation.CurrentNavigation.StructuralNavigation, parser, applyingInformation.ClearNavigation, scope); break; } if (!isNoScriptSite) { navigationSettings.Update(TaxonomySession.GetTaxonomySession(web.Context)); web.Context.ExecuteQueryRetry(); } } } } return(parser); }
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite currentSite = properties.Feature.Parent as SPSite; if (currentSite != null) { SPSecurity.RunWithElevatedPrivileges(delegate() { SPWeb currentWeb = currentSite.RootWeb; InitializeWebAppProperties(currentWeb); //Set the master page to be our CustomMaster page. currentWeb.AllowUnsafeUpdates = true; string masterURL = currentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/MasterPage/CustomMaster.master"; string customMasterURL = currentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/MasterPage/CustomMaster.master"; currentWeb.MasterUrl = masterURL; currentWeb.CustomMasterUrl = customMasterURL; currentWeb.Update(); //Some templates might have subsites from start. foreach (SPWeb subWeb in currentWeb.Site.RootWeb.GetSubwebsForCurrentUser()) { ChangeMasterPage(subWeb, masterURL, customMasterURL); } TaxonomySession taxonomySession = new TaxonomySession(currentWeb.Site, updateCache: true); //Use the first Termstore object to see if Taxonomy Service is offline or missing. if (taxonomySession.TermStores.Count == 0) throw new InvalidOperationException("The Taxonomy Service is offline or missing."); string globalTermSetID = "Empty"; string localTermSetID = "Empty"; //Check property bag for TermSetID key and value. If it doesn't exist create it. if (!currentSite.RootWeb.AllProperties.ContainsKey(globalTermSetIDPropertyKey)) { currentSite.RootWeb.AllProperties.Add(globalTermSetIDPropertyKey, globalTermSetID); currentSite.RootWeb.Update(); } if (!currentSite.RootWeb.AllProperties.ContainsKey(localTermSetIDPropertyKey)) { currentSite.RootWeb.AllProperties.Add(localTermSetIDPropertyKey, localTermSetID); currentSite.RootWeb.Update(); } if (!currentSite.RootWeb.AllProperties.ContainsKey(suiteLinksTermStoreKey)) { currentSite.RootWeb.AllProperties.Add(suiteLinksTermStoreKey, termStoreName); currentSite.RootWeb.Update(); } if (!currentSite.RootWeb.AllProperties.ContainsKey(suiteLinksTermSetID)) { currentSite.RootWeb.AllProperties.Add(suiteLinksTermSetID, termSetID.ToString()); currentSite.RootWeb.Update(); } //Get the values from the property bags. globalTermSetID = Convert.ToString(currentWeb.Site.RootWeb.AllProperties[globalTermSetIDPropertyKey]); localTermSetID = Convert.ToString(currentWeb.Site.RootWeb.AllProperties[localTermSetIDPropertyKey]); TermStore termStore = taxonomySession.TermStores[termStoreName]; if (globalTermSetID == "Empty") { TermSet newTermSet = CreateTermSet(termStore, Guid.NewGuid(), currentWeb, TermStoreType.Global); currentSite.RootWeb.AllProperties[globalTermSetIDPropertyKey] = newTermSet.Id.ToString(); currentSite.RootWeb.Update(); globalTermSetID = newTermSet.Id.ToString(); } if (localTermSetID == "Empty") { TermSet newTermSet = CreateTermSet(termStore, Guid.NewGuid(), currentWeb, TermStoreType.Local); currentSite.RootWeb.AllProperties[localTermSetIDPropertyKey] = newTermSet.Id.ToString(); currentSite.RootWeb.Update(); localTermSetID = newTermSet.Id.ToString(); } TermSet globalTermSet = termStore.GetTermSet(new Guid(globalTermSetID)); TermSet localTermSet = termStore.GetTermSet(new Guid(localTermSetID)); Group termSetGroup = termStore.GetSiteCollectionGroup(currentSite); WebNavigationSettings webNavigationSettings = new WebNavigationSettings(currentWeb); webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = termStore.Id; webNavigationSettings.GlobalNavigation.TermSetId = new Guid(globalTermSetID); webNavigationSettings.CurrentNavigation.TermStoreId = termStore.Id; webNavigationSettings.CurrentNavigation.TermSetId = new Guid(localTermSetID); webNavigationSettings.AddNewPagesToNavigation = false; webNavigationSettings.CreateFriendlyUrlsForNewPages = true; webNavigationSettings.Update(); currentWeb.Update(); }); } }
public void ConfigureTaxonomyNavigation() { ClientContext clientContext = new ClientContext(TestConfig.ServerUrl); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); taxonomySession.UpdateCache(); NavigationTermSet termSet = DemoUtilities.RecreateSampleNavTermSet( this.TestContext, clientContext, taxonomySession, clientContext.Web); // Clear out any old settings WebNavigationSettings webNavigationSettings = new WebNavigationSettings(clientContext, clientContext.Web); webNavigationSettings.ResetToDefaults(); webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(clientContext, clientContext.Site); clientContext.ExecuteQuery(); this.WaitForSync(); // Verify the TermSet is not running NavigationTermSet actualTermSet; ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext); using (scope.StartScope()) { using (scope.StartTry()) { actualTermSet = TaxonomyNavigation.GetTermSetForWeb(clientContext, clientContext.Web, "GlobalNavigationTaxonomyProvider", includeInheritedSettings: true); } using (scope.StartCatch()) { } } clientContext.ExecuteQuery(); Assert.IsTrue(actualTermSet.ServerObjectIsNull.Value); // Assign the new settings webNavigationSettings = new WebNavigationSettings(clientContext, clientContext.Web); clientContext.Load(webNavigationSettings, w => w.GlobalNavigation, w => w.CurrentNavigation ); clientContext.Load(termSet, ts => ts.TermStoreId, ts => ts.Id ); clientContext.ExecuteQuery(); // GlobalNavigation = top menu (aka "top nav") // CurrentNavigation = left menu (aka "quick launch") webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = termSet.TermStoreId; webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id; webNavigationSettings.Update(taxonomySession); TaxonomyNavigation.FlushSiteFromCache(clientContext, clientContext.Site); clientContext.ExecuteQuery(); this.WaitForSync(); actualTermSet = TaxonomyNavigation.GetTermSetForWeb(clientContext, clientContext.Web, "GlobalNavigationTaxonomyProvider", includeInheritedSettings: true); clientContext.Load(actualTermSet, ts => ts.Id); clientContext.ExecuteQuery(); Assert.AreEqual(termSet.Id, actualTermSet.Id); }
/// <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 var taxonomySession = TaxonomySession.GetTaxonomySession(web.Context); web.Context.Load(taxonomySession); web.Context.ExecuteQueryRetry(); var webNav = new WebNavigationSettings(web.Context, web); if (!navigationSettings.GlobalNavigation.ManagedNavigation) { webNav.GlobalNavigation.Source = StandardNavigationSource.PortalProvider; } else { webNav.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { webNav.CurrentNavigation.Source = StandardNavigationSource.PortalProvider; } else { webNav.CurrentNavigation.Source = 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) { var globalNavigationIncludeType = MapToNavigationIncludeTypes(navigationSettings.GlobalNavigation); web.AllProperties[GlobalNavigationIncludeTypes] = globalNavigationIncludeType; web.AllProperties[GlobalDynamicChildLimit] = navigationSettings.GlobalNavigation.MaxDynamicItems; } if (!navigationSettings.CurrentNavigation.ManagedNavigation) { var 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(); }
public static void RePin() { Guid termSetGUID = new Guid("7ab9e8b0-e1e1-4a7c-9b20-d6c5030103df"); Guid srcTermSetGUID = new Guid("4d8916d8-e226-45f6-83bd-9f2b134cc264"); Guid srcDepartmentGUID = new Guid("eea07c73-7e2b-418d-96ff-1a2cdb4eb25c"); string siteUrl = "http://win-f33ohjutmmi/sites/cms"; ClientContext clientContext = new ClientContext(siteUrl); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); taxonomySession.UpdateCache(); clientContext.Load(taxonomySession, ts => ts.TermStores); clientContext.ExecuteQuery(); if (taxonomySession.TermStores.Count == 0) { throw new InvalidOperationException("The Taxonomy Service is offline or missing"); } TermStore termStore = taxonomySession.TermStores[0]; clientContext.Load(termStore, ts => ts.Name, ts => ts.WorkingLanguage); clientContext.ExecuteQuery(); // Does the TermSet object already exist? TermSet existingTermSet; TermGroup siteCollectionGroup; siteCollectionGroup = termStore.GetSiteCollectionGroup(clientContext.Site, createIfMissing: true); existingTermSet = termStore.GetTermSet(termSetGUID); clientContext.Load(existingTermSet); clientContext.ExecuteQuery(); if (!existingTermSet.ServerObjectIsNull.Value) { existingTermSet.DeleteObject(); termStore.CommitAll(); clientContext.ExecuteQuery(); } TermSet termSet = siteCollectionGroup.CreateTermSet("CMSNavigationTermSet", termSetGUID, termStore.WorkingLanguage); clientContext.Load(termSet); termStore.CommitAll(); clientContext.ExecuteQuery(); //*******below code does not work, so commented it out*******// //NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(clientContext, // termSet, clientContext.Web, "GlobalNavigationTaxonomyProvider"); //navTermSet.IsNavigationTermSet = true; //termStore.CommitAll(); //clientContext.ExecuteQuery(); //******* *******// Term srcTerm = null; Term targetTerm = null; //get the source termset srcTerm = termStore.GetTerm(srcDepartmentGUID); clientContext.Load(srcTerm); clientContext.ExecuteQuery(); if (!srcTerm.ServerObjectIsNull.Value) { targetTerm = termSet.ReuseTermWithPinning(srcTerm); targetTerm.CustomSortOrder = srcTerm.CustomSortOrder; termStore.CommitAll(); clientContext.ExecuteQuery(); } WebNavigationSettings navigationSettings = new WebNavigationSettings(clientContext, clientContext.Web); //******* this is weird, we have to reset the navigation to something else, then change it back to TaxonomyProvider, this way works. navigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider; navigationSettings.Update(taxonomySession); clientContext.ExecuteQuery(); //*********************************// navigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; navigationSettings.GlobalNavigation.TermStoreId = termStore.Id; navigationSettings.GlobalNavigation.TermSetId = termSet.Id; navigationSettings.Update(taxonomySession); //clientContext.Load(navigationSettings); clientContext.ExecuteQuery(); }
private static void ManagedRePin() { using (SPSite site = new SPSite("http://win-f33ohjutmmi/sites/cms")) { using (SPWeb web = site.OpenWeb()) { TaxonomySession _TaxonomySession = new TaxonomySession(site); //Get instance of the Term Store TermStore _TermStore = _TaxonomySession.TermStores["Managed Metadata Service"]; Group _Group = _TermStore.GetSiteCollectionGroup(site); ////Create a new Term Set in the new Group TermSet _TermSet = null; try { _TermSet = _Group.TermSets["CMSNavigationTermSet"]; } catch { } if (_TermSet != null) { //_TermSet.Delete(); //_TermStore.CommitAll(); _TermSet.Terms["Department"].Delete(); _TermStore.CommitAll(); } else { _TermSet = _Group.CreateTermSet("CMSNavigationTermSet"); NavigationTermSet navigationTermSet = NavigationTermSet.GetAsResolvedByWeb(_TermSet, site.RootWeb, StandardNavigationProviderNames.CurrentNavigationTaxonomyProvider); navigationTermSet.IsNavigationTermSet = true; } //locate the term in source farm var srcgroup = from g in _TermStore.Groups where g.Name == "NavigationGroup" select g; var srctermSet = srcgroup.FirstOrDefault().TermSets["GlobalNav"]; var srcterm = srctermSet.Terms["Department"]; var newterm = _TermSet.ReuseTermWithPinning(srcterm); newterm.CustomSortOrder = srcterm.CustomSortOrder; //commit changes _TermStore.CommitAll(); var webNavigationSettings = new WebNavigationSettings(web); webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider; webNavigationSettings.GlobalNavigation.TermStoreId = _TermStore.Id; webNavigationSettings.GlobalNavigation.TermSetId = _TermSet.Id; webNavigationSettings.Update(); var pubWeb = PublishingWeb.GetPublishingWeb(web); pubWeb.Update(); web.Update(); } } }