protected override void ProcessFieldProperties(Field field, FieldDefinition fieldModel) { var context = CurrentSiteModelHost.HostClientContext; var taxFieldModel = fieldModel.WithAssertAndCast <TaxonomyFieldDefinition>("model", value => value.RequireNotNull()); var taxField = context.CastTo <TaxonomyField>(field); // let base setting be setup base.ProcessFieldProperties(taxField, fieldModel); context.ExecuteQueryWithTrace(); var termStore = TaxonomyTermStoreModelHandler.FindTermStore(CurrentSiteModelHost, taxFieldModel.SspName, taxFieldModel.SspId, taxFieldModel.UseDefaultSiteCollectionTermStore); if (termStore == null) { throw new ArgumentNullException("termStore is NULL. Please define SspName, SspId or ensure there is a default term store for the giving site."); } var storeContext = CurrentSiteModelHost.HostClientContext; storeContext.Load(termStore, s => s.Id); storeContext.ExecuteQueryWithTrace(); // TODO TermSet termSet = null; if (!string.IsNullOrEmpty(taxFieldModel.TermSetName)) { var termSets = termStore.GetTermSetsByName(taxFieldModel.TermSetName, taxFieldModel.TermSetLCID); storeContext.Load(termSets); storeContext.ExecuteQueryWithTrace(); termSet = termSets.FirstOrDefault(); } if (termStore != null) { taxField.SspId = termStore.Id; } if (termSet != null) { taxField.TermSetId = termSet.Id; } }
public static TermStore LookupTermStore(SiteModelHost currentSiteModelHost, TaxonomyFieldDefinition taxFieldModel) { var termStore = TaxonomyTermStoreModelHandler.FindTermStore(currentSiteModelHost, taxFieldModel.SspName, taxFieldModel.SspId, taxFieldModel.UseDefaultSiteCollectionTermStore); if (termStore == null) { throw new ArgumentNullException("termStore is NULL. Please define SspName, SspId or ensure there is a default term store for the giving site."); } var storeContext = currentSiteModelHost.HostClientContext; storeContext.Load(termStore, s => s.Id); storeContext.ExecuteQueryWithTrace(); return(termStore); }
private void DeployNavigationSettings(object modelHost, WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel) { var shouldUpdateWeb = false; var site = webModelHost.HostSite; var web = webModelHost.HostWeb; var context = web.Context; var thisWebNavSettings = GetWebNavigationSettings(webModelHost, navigationModel); StandardNavigationSource?globalSource = null; StandardNavigationSource?currentSource = null; if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource)) { globalSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.GlobalNavigationSource); } if (!string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { currentSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.CurrentNavigationSource); } TermStore currentTermStore = null; TermSet currentTermSet = null; if (currentSource == StandardNavigationSource.TaxonomyProvider) { currentTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site, navigationModel.CurrentNavigationTermStoreName, navigationModel.CurrentNavigationTermStoreId, navigationModel.CurrentNavigationUseDefaultSiteCollectionTermStore); currentTermSet = TaxonomyFieldModelHandler.LookupTermSet(currentTermStore.Context, currentTermStore, site, null, null, null, navigationModel.CurrentNavigationTermSetName, navigationModel.CurrentNavigationTermSetId, navigationModel.CurrentNavigationTermSetLCID); } TermStore globalTermStore = null; TermSet globalTermSet = null; if (globalSource == StandardNavigationSource.TaxonomyProvider) { globalTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site, navigationModel.GlobalNavigationTermStoreName, navigationModel.GlobalNavigationTermStoreId, navigationModel.GlobalNavigationUseDefaultSiteCollectionTermStore); globalTermSet = TaxonomyFieldModelHandler.LookupTermSet(site.Context, globalTermStore, site, null, null, null, navigationModel.GlobalNavigationTermSetName, navigationModel.GlobalNavigationTermSetId, navigationModel.GlobalNavigationTermSetLCID); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = thisWebNavSettings, ObjectType = typeof(WebNavigationSettings), ObjectDefinition = navigationModel, ModelHost = modelHost }); if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) || !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { if (globalSource.HasValue) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalNavigation to: [{0}]", navigationModel.GlobalNavigationSource); thisWebNavSettings.GlobalNavigation.Source = globalSource.Value; if (globalTermStore != null) { thisWebNavSettings.GlobalNavigation.TermStoreId = globalTermStore.Id; thisWebNavSettings.GlobalNavigation.TermSetId = globalTermSet.Id; } else { int?globalNavigationIncludeTypes = GetGlobalNavigationIncludeTypes(navigationModel); if (globalNavigationIncludeTypes != null) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalNavigationIncludeTypes to: [{0}]", globalNavigationIncludeTypes); web.AllProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes] = globalNavigationIncludeTypes; shouldUpdateWeb = true; } if (navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.HasValue) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting GlobalDynamicChildLimit to: [{0}]", navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value); web.AllProperties[BuiltInWebPropertyId.GlobalDynamicChildLimit] = navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value; shouldUpdateWeb = true; } } } if (currentSource.HasValue) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting CurrentNavigation to: [{0}]", navigationModel.CurrentNavigationSource); thisWebNavSettings.CurrentNavigation.Source = currentSource.Value; if (currentTermStore != null) { thisWebNavSettings.CurrentNavigation.TermStoreId = currentTermStore.Id; thisWebNavSettings.CurrentNavigation.TermSetId = currentTermSet.Id; } else { int?currentNavigationIncludeTypes = GetCurrentNavigationIncludeTypes(navigationModel); if (currentNavigationIncludeTypes != null) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting CurrentNavigationIncludeTypes to: [{0}]", currentNavigationIncludeTypes); web.AllProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes] = currentNavigationIncludeTypes; shouldUpdateWeb = true; } if (navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.HasValue) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Setting CurrentDynamicChildLimit to: [{0}]", navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value); web.AllProperties[BuiltInWebPropertyId.CurrentDynamicChildLimit] = navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value; shouldUpdateWeb = true; } } } } if (navigationModel.DisplayShowHideRibbonAction.HasValue) { web.AllProperties["__DisplayShowHideRibbonActionId"] = navigationModel.DisplayShowHideRibbonAction.ToString(); shouldUpdateWeb = true; } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = thisWebNavSettings, ObjectType = typeof(WebNavigationSettings), ObjectDefinition = navigationModel, ModelHost = modelHost }); if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) || !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating navigation settings"); thisWebNavSettings.Update(null); context.ExecuteQueryWithTrace(); } // update include types if (shouldUpdateWeb) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating web"); web.Update(); context.ExecuteQueryWithTrace(); } }
private void DeployNavigationSettings(object modelHost, WebModelHost webModelHost, WebNavigationSettingsDefinition navigationModel) { var site = webModelHost.HostWeb.Site; var web = webModelHost.HostWeb; var thisWebNavSettings = GetWebNavigationSettings(webModelHost, navigationModel); var shouldUpdateWeb = false; var allProperties = web.AllProperties; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = thisWebNavSettings, ObjectType = typeof(WebNavigationSettings), ObjectDefinition = navigationModel, ModelHost = modelHost }); if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) || !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource)) { var globalSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.GlobalNavigationSource); thisWebNavSettings.GlobalNavigation.Source = globalSource; if (globalSource == StandardNavigationSource.TaxonomyProvider) { var globalTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site, navigationModel.GlobalNavigationTermStoreName, navigationModel.GlobalNavigationTermStoreId, navigationModel.GlobalNavigationUseDefaultSiteCollectionTermStore); var globalTermSet = TaxonomyFieldModelHandler.LookupTermSet(globalTermStore, null, null, null, null, navigationModel.GlobalNavigationTermSetName, navigationModel.GlobalNavigationTermSetId, navigationModel.GlobalNavigationTermSetLCID); thisWebNavSettings.GlobalNavigation.TermStoreId = globalTermStore.Id; thisWebNavSettings.GlobalNavigation.TermSetId = globalTermSet.Id; } else { int?globalNavigationIncludeTypes = GetGlobalNavigationIncludeTypes(navigationModel, ConvertUtils.ToInt(allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes])); if (globalNavigationIncludeTypes != null) { allProperties[BuiltInWebPropertyId.GlobalNavigationIncludeTypes] = globalNavigationIncludeTypes.Value; shouldUpdateWeb = true; } if (navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.HasValue) { allProperties[BuiltInWebPropertyId.GlobalDynamicChildLimit] = navigationModel.GlobalNavigationMaximumNumberOfDynamicItems.Value; shouldUpdateWeb = true; } } } if (!string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { var currentSource = (StandardNavigationSource)Enum.Parse(typeof(StandardNavigationSource), navigationModel.CurrentNavigationSource); thisWebNavSettings.CurrentNavigation.Source = currentSource; if (currentSource == StandardNavigationSource.TaxonomyProvider) { var currentTermStore = TaxonomyTermStoreModelHandler.FindTermStore(site, navigationModel.CurrentNavigationTermStoreName, navigationModel.CurrentNavigationTermStoreId, navigationModel.CurrentNavigationUseDefaultSiteCollectionTermStore); var currentTermSet = TaxonomyFieldModelHandler.LookupTermSet(currentTermStore, null, null, null, null, navigationModel.CurrentNavigationTermSetName, navigationModel.CurrentNavigationTermSetId, navigationModel.CurrentNavigationTermSetLCID); thisWebNavSettings.CurrentNavigation.TermStoreId = currentTermStore.Id; thisWebNavSettings.CurrentNavigation.TermSetId = currentTermSet.Id; } else { int?currentNavigationIncludeTypes = GetCurrentNavigationIncludeTypes(navigationModel, ConvertUtils.ToInt(allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes])); if (currentNavigationIncludeTypes != null) { allProperties[BuiltInWebPropertyId.CurrentNavigationIncludeTypes] = currentNavigationIncludeTypes.Value; shouldUpdateWeb = true; } if (navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.HasValue) { allProperties[BuiltInWebPropertyId.CurrentDynamicChildLimit] = navigationModel.CurrentNavigationMaximumNumberOfDynamicItems.Value; shouldUpdateWeb = true; } } } } if (navigationModel.AddNewPagesToNavigation.HasValue) { thisWebNavSettings.AddNewPagesToNavigation = navigationModel.AddNewPagesToNavigation.Value; } if (navigationModel.CreateFriendlyUrlsForNewPages.HasValue) { thisWebNavSettings.CreateFriendlyUrlsForNewPages = navigationModel.CreateFriendlyUrlsForNewPages.Value; } if (navigationModel.DisplayShowHideRibbonAction.HasValue) { allProperties[BuiltInWebPropertyId.DisplayShowHideRibbonActionId] = navigationModel.DisplayShowHideRibbonAction.ToString(); shouldUpdateWeb = true; } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = thisWebNavSettings, ObjectType = typeof(WebNavigationSettings), ObjectDefinition = navigationModel, ModelHost = modelHost }); if (shouldUpdateWeb) { web.Update(); } if (!string.IsNullOrEmpty(navigationModel.GlobalNavigationSource) || !string.IsNullOrEmpty(navigationModel.CurrentNavigationSource)) { thisWebNavSettings.Update(null); } }