protected SPGroup ResolveSecurityGroup(SPWeb web, SecurityGroupLinkDefinition securityGroupLinkModel) { SPGroup securityGroup = null; if (securityGroupLinkModel.IsAssociatedMemberGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup = true. Resolving AssociatedMemberGroup"); securityGroup = web.AssociatedMemberGroup; } else if (securityGroupLinkModel.IsAssociatedOwnerGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedOwnerGroup = true. Resolving IsAssociatedOwnerGroup"); securityGroup = web.AssociatedOwnerGroup; } else if (securityGroupLinkModel.IsAssociatedVisitorGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedVisitorGroup = true. Resolving IsAssociatedVisitorGroup"); securityGroup = web.AssociatedVisitorGroup; } else if (!string.IsNullOrEmpty(securityGroupLinkModel.SecurityGroupName)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving group by name: [{0}]", securityGroupLinkModel.SecurityGroupName); securityGroup = web.SiteGroups[securityGroupLinkModel.SecurityGroupName]; } else { TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup/IsAssociatedOwnerGroup/IsAssociatedVisitorGroup/SecurityGroupName should be defined. Throwing SPMeta2Exception"); throw new SPMeta2Exception("securityGroupLinkModel"); } return(securityGroup); }
protected override void ProcessSPFieldXElement(XElement fieldTemplate, FieldDefinition fieldModel) { base.ProcessSPFieldXElement(fieldTemplate, fieldModel); var typedFieldModel = fieldModel.WithAssertAndCast <CalculatedFieldDefinition>("model", value => value.RequireNotNull()); if (typedFieldModel.CurrencyLocaleId.HasValue) { fieldTemplate.SetAttribute(BuiltInFieldAttributes.LCID, typedFieldModel.CurrencyLocaleId); } if (!string.IsNullOrEmpty(typedFieldModel.Formula)) { // can't really validate it automatically // Improve CalculatedFieldDefinition with field ref check // https://github.com/SubPointSolutions/spmeta2/issues/648 TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Crafting formula for a CalculatedField. Ensure FieldReferences are correct."); // should be a new XML node var formulaNode = new XElement(BuiltInFieldAttributes.Formula, typedFieldModel.Formula); fieldTemplate.Add(formulaNode); // Format="0" when provisioning CalculatedField #969 // https://github.com/SubPointSolutions/spmeta2/issues/969 //fieldTemplate.SetAttribute(BuiltInFieldAttributes.Format, // (int)Enum.Parse(typeof(SPDateTimeFieldFormatType), typedFieldModel.DateFormat)); fieldTemplate.SetAttribute(BuiltInFieldAttributes.Format, typedFieldModel.DateFormat); } if (typedFieldModel.ShowAsPercentage.HasValue) { fieldTemplate.SetAttribute(BuiltInFieldAttributes.Percentage, typedFieldModel.ShowAsPercentage.Value.ToString().ToUpper()); } if (!string.IsNullOrEmpty(typedFieldModel.DisplayFormat)) { fieldTemplate.SetAttribute(BuiltInFieldAttributes.Decimals, NumberFieldModelHandler.GetDecimalsValue(typedFieldModel.DisplayFormat)); } fieldTemplate.SetAttribute(BuiltInFieldAttributes.ResultType, typedFieldModel.OutputType); if (typedFieldModel.FieldReferences.Count > 0) { var fieldRefsNode = new XElement("FieldRefs"); foreach (var fieldRef in typedFieldModel.FieldReferences) { var fieldRefNode = new XElement("FieldRef"); fieldRefNode.SetAttribute("Name", fieldRef); fieldRefsNode.Add(fieldRefNode); } fieldTemplate.Add(fieldRefsNode); } }
private void ProcessRoleInheritance(object modelHost, SecurableObject securableObject, BreakRoleInheritanceDefinition breakRoleInheritanceModel) { var context = securableObject.Context; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = securableObject, ObjectType = typeof(SecurableObject), ObjectDefinition = breakRoleInheritanceModel, ModelHost = modelHost }); if (!securableObject.IsObjectPropertyInstantiated("HasUniqueRoleAssignments")) { context.Load(securableObject, s => s.HasUniqueRoleAssignments); context.ExecuteQueryWithTrace(); } if (!securableObject.HasUniqueRoleAssignments) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "HasUniqueRoleAssignments is FALSE. Breaking role inheritance with CopyRoleAssignments: [{0}] and ClearSubscopes: [{1}]", new object[] { breakRoleInheritanceModel.CopyRoleAssignments, breakRoleInheritanceModel.ClearSubscopes }); securableObject.BreakRoleInheritance(breakRoleInheritanceModel.CopyRoleAssignments, breakRoleInheritanceModel.ClearSubscopes); context.ExecuteQueryWithTrace(); } if (breakRoleInheritanceModel.ForceClearSubscopes) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ForceClearSubscopes is TRUE. Removing all role assignments."); context.Load(securableObject.RoleAssignments); context.ExecuteQueryWithTrace(); while (securableObject.RoleAssignments.Count > 0) { securableObject.RoleAssignments[0].DeleteObject(); } } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = securableObject, ObjectType = typeof(SecurableObject), ObjectDefinition = breakRoleInheritanceModel, ModelHost = modelHost }); }
protected SPWeb GetOrCreateWeb(SPWeb parentWeb, WebDefinition webModel, bool updateProperties) { var webUrl = webModel.Url; var webDescription = string.IsNullOrEmpty(webModel.Description) ? String.Empty : webModel.Description; var currentWeb = GetWeb(parentWeb, webModel); if (!currentWeb.Exists) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = null, ObjectType = typeof(SPWeb), ObjectDefinition = webModel, ModelHost = webModel }); currentWeb = parentWeb.Webs.Add(webUrl, webModel.Title, webDescription, webModel.LCID, webModel.WebTemplate, webModel.UseUniquePermission, webModel.ConvertIfThere); } else { if (updateProperties) { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Current web is not null. Updating Title/Description."); currentWeb.Title = webModel.Title; currentWeb.Description = webModel.Description ?? string.Empty; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentWeb, ObjectType = typeof(SPWeb), ObjectDefinition = webModel, ModelHost = webModel }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "currentWeb.Update()"); currentWeb.Update(); } } return(currentWeb); }
private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel) { var siteModelHost = modelHost.WithAssertAndCast <SiteModelHost>("modelHost", value => value.RequireNotNull()); var site = siteModelHost.HostSite; TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying site feature."); ProcessFeature(modelHost, site.Features, featureModel); }
private void DeployWebApplicationFeature(object modelHost, FeatureDefinition featureModel) { var webApplicationModelHost = modelHost.WithAssertAndCast <WebApplicationModelHost>("modelHost", value => value.RequireNotNull()); var webApplication = webApplicationModelHost.HostWebApplication; TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying web application feature."); ProcessFeature(modelHost, webApplication.Features, featureModel); }
private void DeployTermLabel(object modelHost, TermModelHost termModelHost, TaxonomyTermLabelDefinition labelModel) { var termStore = termModelHost.HostTermStore; var context = termStore.Context; var term = termModelHost.HostTerm; var currentLabel = FindLabelInTerm(term, labelModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentLabel, ObjectType = typeof(Label), ObjectDefinition = labelModel, ModelHost = modelHost }); if (currentLabel == null) { TraceService.Verbose((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Label"); currentLabel = term.CreateLabel(labelModel.Name, labelModel.LCID, labelModel.IsDefault); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentLabel, ObjectType = typeof(Label), ObjectDefinition = labelModel, ModelHost = modelHost }); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentLabel, ObjectType = typeof(Label), ObjectDefinition = labelModel, ModelHost = modelHost }); } termStore.CommitAll(); context.ExecuteQueryWithTrace(); }
private void DeployFarmFeature(object modelHost, FeatureDefinition featureModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying farm feature."); var farmModelHost = modelHost.WithAssertAndCast <FarmModelHost>("modelHost", value => value.RequireNotNull()); var farm = farmModelHost.HostFarm; var adminService = SPWebService.AdministrationService; ProcessFeature(modelHost, adminService.Features, featureModel); }
private Field DeployWebField(WebModelHost webModelHost, FieldDefinition fieldModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying web field"); var web = webModelHost.HostWeb; var context = web.Context; var field = GetField(webModelHost, fieldModel); return(EnsureField(context, field, web.Fields, fieldModel)); }
private Field DeploySiteField(SiteModelHost siteModelHost, FieldDefinition fieldModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying site field"); var site = siteModelHost.HostSite; var context = site.Context; var field = GetField(siteModelHost, fieldModel); return(EnsureField(context, field, site.RootWeb.Fields, fieldModel)); }
public static void ExecuteQueryWithTrace(this ClientRuntimeContext context) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); // delegating to ClientRuntimeContextService instance // Implement fault tolerant provision for CSOM #567 // https://github.com/SubPointSolutions/spmeta2/issues/567 //context.ExecuteQuery(); ClientRuntimeContextService.ExecuteQuery(context); }
protected override SPList CreateObject(WebModelHost typedModelHost, ListDefinition definition) { var listModel = definition; var web = typedModelHost.HostWeb; TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list"); var listId = default(Guid); // create with the random title to avoid issue with 2 lists + diff URL and same Title // list Title will be renamed later on var listTitle = Guid.NewGuid().ToString("N"); // "SPBug", there are two ways to create lists // (1) by TemplateName (2) by TemplateType if (listModel.TemplateType > 0) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Creating list by TemplateType: [{0}]", listModel.TemplateType); //listId = web.Lists.Add(listModel.Url, listModel.Description ?? string.Empty, (SPListTemplateType)listModel.TemplateType); listId = web.Lists.Add( listTitle, listModel.Description ?? string.Empty, #pragma warning disable 618 listModel.GetListUrl(), #pragma warning restore 618 string.Empty, (int)listModel.TemplateType, string.Empty); } else if (!string.IsNullOrEmpty(listModel.TemplateName)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Creating list by TemplateName: [{0}]", listModel.TemplateName); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Searching list template in web.ListTemplates"); var listTemplate = ResolveListTemplate(web, listModel); listId = web.Lists.Add( listTitle, listModel.Description ?? string.Empty, #pragma warning disable 618 listModel.GetListUrl(), #pragma warning restore 618 listTemplate.FeatureId.ToString(), (int)listTemplate.Type, listTemplate.DocumentTemplate); } else { throw new ArgumentException("TemplateType or TemplateName must be defined"); } return(web.Lists[listId]); }
protected Web GetExistingWeb(Site site, Web parentWeb, string currentWebUrl) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Entering GetExistingWeb()"); var result = false; var srcUrl = currentWebUrl.ToLower().Trim('/').Trim('\\'); // for self-hosting and '/' if (parentWeb.Url.ToLower().Trim('/').Trim('\\').EndsWith(srcUrl)) { return(parentWeb); } var context = parentWeb.Context; Web web = null; var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { web = site.OpenWeb(currentWebUrl); } using (scope.StartCatch()) { } } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); if (!scope.HasException && web != null && web.ServerObjectIsNull == false) { TraceService.InformationFormat((int)LogEventId.ModelProvisionCoreCall, "Found web with URL: [{0}]", currentWebUrl); context.Load(web); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Exciting GetExistingWeb()"); return(web); } TraceService.InformationFormat((int)LogEventId.ModelProvisionCoreCall, "Can't find web with URL: [{0}]. Returning NULL.", currentWebUrl); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Exciting GetExistingWeb()"); return(null); }
private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel) { var siteModelHost = modelHost.WithAssertAndCast <SiteModelHost>("modelHost", value => value.RequireNotNull()); var site = siteModelHost.HostSite; var context = site.Context; TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying site feature."); // a bit unclear why it should be Farm scope // seems to be a scope to find feature definition in so that for sandbox solutions it would be Site? ProcessFeature(modelHost, context, site.Features, featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope.Farm); }
private void MapCustomAction(SPUserCustomAction existringAction, UserCustomActionDefinition customAction) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating user custom action properties."); existringAction.Description = customAction.Description; existringAction.Group = customAction.Group; existringAction.Location = customAction.Location; existringAction.Name = customAction.Name; existringAction.ScriptBlock = customAction.ScriptBlock; existringAction.ScriptSrc = customAction.ScriptSrc; existringAction.Title = customAction.Title; existringAction.Url = customAction.Url; existringAction.Sequence = customAction.Sequence; if (!string.IsNullOrEmpty(customAction.CommandUIExtension)) { existringAction.CommandUIExtension = customAction.CommandUIExtension; } if (!string.IsNullOrEmpty(customAction.RegistrationId)) { existringAction.RegistrationId = customAction.RegistrationId; } if (!string.IsNullOrEmpty(customAction.RegistrationType)) { // skipping setup for List script // System.NotSupportedException: Setting this property is not supported. A value of List has already been set and cannot be changed. if (existringAction.RegistrationType != SPUserCustomActionRegistrationType.List) { existringAction.RegistrationType = (SPUserCustomActionRegistrationType) Enum.Parse(typeof(SPUserCustomActionRegistrationType), customAction.RegistrationType, true); } } var permissions = SPBasePermissions.EmptyMask; if (customAction.Rights != null && customAction.Rights.Count > 0) { foreach (var permissionString in customAction.Rights) { permissions = permissions | (SPBasePermissions)Enum.Parse(typeof(SPBasePermissions), permissionString); } } existringAction.Rights = permissions; ProcessLocalization(existringAction, customAction); }
private void DeployWebFeature(object modelHost, FeatureDefinition featureModel) { var webModelHost = modelHost.WithAssertAndCast <WebModelHost>("modelHost", value => value.RequireNotNull()); var web = webModelHost.HostWeb; var context = web.Context; TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying web feature."); // a bit unclear why it should be Farm scope // seems to be a scope to find feature definition in so that for sandbox solutions it would be Site? // http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.featurecollection.add%28v=office.15%29.aspx ProcessFeature(modelHost, context, web.Features, featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope.Farm); }
private void ProcessRoleInheritance(object modelHost, SPSecurableObject securableObject, BreakRoleInheritanceDefinition breakRoleInheritanceModel) { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = securableObject, ObjectType = typeof(SPSecurableObject), ObjectDefinition = breakRoleInheritanceModel, ModelHost = modelHost }); if (!securableObject.HasUniqueRoleAssignments) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "HasUniqueRoleAssignments is FALSE. Breaking role inheritance with CopyRoleAssignments: [{0}] and ClearSubscopes: [{1}]", new object[] { breakRoleInheritanceModel.CopyRoleAssignments, breakRoleInheritanceModel.ClearSubscopes }); securableObject.BreakRoleInheritance(breakRoleInheritanceModel.CopyRoleAssignments, breakRoleInheritanceModel.ClearSubscopes); } if (breakRoleInheritanceModel.ForceClearSubscopes) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ForceClearSubscopes is TRUE. Removing all role assignments."); while (securableObject.RoleAssignments.Count > 0) { securableObject.RoleAssignments.Remove(0); } } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = securableObject, ObjectType = typeof(SPSecurableObject), ObjectDefinition = breakRoleInheritanceModel, ModelHost = modelHost }); }
private void DeploySiteCustomAction(object modelHost, UserCustomActionDefinition model) { UserCustomActionCollection userCustomActions = null; var existingAction = GetCurrentCustomUserAction(modelHost, model, out userCustomActions); var context = userCustomActions.Context; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = null, ObjectType = typeof(UserCustomAction), ObjectDefinition = model, ModelHost = modelHost }); if (existingAction == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new user custom action"); existingAction = userCustomActions.Add(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing user custom action"); } MapCustomAction(existingAction, model); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingAction, ObjectType = typeof(UserCustomAction), ObjectDefinition = model, ModelHost = modelHost }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling existingAction.Update()"); existingAction.Update(); context.ExecuteQueryWithTrace(); }
protected virtual Group ResolveSecurityGroup(SecurityGroupLinkDefinition securityGroupLinkModel, Web web, ClientRuntimeContext context) { Group securityGroup = null; if (securityGroupLinkModel.IsAssociatedMemberGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup = true. Resolving AssociatedMemberGroup"); context.Load(web, w => w.AssociatedMemberGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedMemberGroup; } else if (securityGroupLinkModel.IsAssociatedOwnerGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedOwnerGroup = true. Resolving IsAssociatedOwnerGroup"); context.Load(web, w => w.AssociatedOwnerGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedOwnerGroup; } else if (securityGroupLinkModel.IsAssociatedVisitorGroup) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedVisitorGroup = true. Resolving IsAssociatedVisitorGroup"); context.Load(web, w => w.AssociatedVisitorGroup); context.ExecuteQueryWithTrace(); securityGroup = web.AssociatedVisitorGroup; } else if (!string.IsNullOrEmpty(securityGroupLinkModel.SecurityGroupName)) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving group by name: [{0}]", securityGroupLinkModel.SecurityGroupName); securityGroup = WebExtensions.FindGroupByName(web.SiteGroups, securityGroupLinkModel.SecurityGroupName); } else { TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "IsAssociatedMemberGroup/IsAssociatedOwnerGroup/IsAssociatedVisitorGroup/SecurityGroupName should be defined. Throwing SPMeta2Exception"); throw new SPMeta2Exception("securityGroupLinkModel"); } return(securityGroup); }
public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action <object> action) { var siteModelHost = modelHost.WithAssertAndCast <SiteModelHost>("model", value => value.RequireNotNull()); var termStoreModel = model.WithAssertAndCast <TaxonomyTermStoreDefinition>("model", value => value.RequireNotNull()); var termStore = FindTermStore(siteModelHost, termStoreModel); action(new TermStoreModelHost { HostTermStore = termStore }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling termStore.CommitAll()"); termStore.CommitAll(); termStore.Context.ExecuteQueryWithTrace(); }
private Field DeployWebField(WebModelHost webModelHost, FieldDefinition fieldDefinition) { var id = fieldDefinition.Id; var web = webModelHost.HostWeb; TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "FindExistingWebField with Id: [{0}]", id); var context = web.Context; var scope = new ExceptionHandlingScope(context); Field field = null; using (scope.StartScope()) { using (scope.StartTry()) { web.Fields.GetById(id); } using (scope.StartCatch()) { } } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); if (!scope.HasException) { field = web.Fields.GetById(id); context.Load(field); PreloadProperties(field); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Found site field with Id: [{0}]", id); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site field with Id: [{0}]", id); } return(EnsureField(context, field, web.Fields, fieldDefinition)); }
protected Field FindExistingSiteField(SiteModelHost siteHost, FieldDefinition fieldDefinition) { var id = fieldDefinition.Id; var rootWeb = siteHost.HostSite.RootWeb; TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "FindExistingSiteField with Id: [{0}]", id); var context = rootWeb.Context; var scope = new ExceptionHandlingScope(context); Field field = null; using (scope.StartScope()) { using (scope.StartTry()) { rootWeb.Fields.GetById(id); } using (scope.StartCatch()) { } } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); if (!scope.HasException) { field = rootWeb.Fields.GetById(id); context.Load(field); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Found site field with Id: [{0}]", id); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site field with Id: [{0}]", id); } return(field); }
private Field DeployListField(ListModelHost modelHost, FieldDefinition fieldModel) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying list field"); var list = modelHost.HostList; var context = list.Context; var scope = new ExceptionHandlingScope(context); Field field; using (scope.StartScope()) { using (scope.StartTry()) { field = list.Fields.GetById(fieldModel.Id); context.Load(field); } using (scope.StartCatch()) { } } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); if (!scope.HasException) { field = list.Fields.GetById(fieldModel.Id); context.Load(field); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Found site list with Id: [{0}]", fieldModel.Id); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); return(EnsureField(context, field, list.Fields, fieldModel)); } TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find list field with Id: [{0}]", fieldModel.Id); return(EnsureField(context, null, list.Fields, fieldModel)); }
protected Field FindExistingListField(List list, FieldDefinition fieldModel) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "FindListField with Id: [{0}]", fieldModel.Id); var context = list.Context; Field field; var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { field = list.Fields.GetById(fieldModel.Id); context.Load(field); } using (scope.StartCatch()) { field = null; //context.Load(field); } } context.ExecuteQueryWithTrace(); if (!scope.HasException) { field = list.Fields.GetById(fieldModel.Id); context.Load(field); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Found list field with Id: [{0}]", fieldModel.Id); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find list field with Id: [{0}]", fieldModel.Id); } return(field); }
private void DeployWelcomePage(object modelHost, DefinitionBase model, Folder folder, WelcomePageDefinition welcomePgaeModel) { var context = folder.Context; context.Load(folder); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = folder, ObjectType = typeof(Folder), ObjectDefinition = welcomePgaeModel, ModelHost = modelHost }); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Changing welcome page to: [{0}]", welcomePgaeModel.Url); // Enhance 'WelcomePageDefinition' - handle / in Url prop #431 // https://github.com/SubPointSolutions/spmeta2/issues/431 folder.WelcomePage = UrlUtility.RemoveStartingSlash(welcomePgaeModel.Url); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = folder, ObjectType = typeof(Folder), ObjectDefinition = welcomePgaeModel, ModelHost = modelHost }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling folder.Update()"); folder.Update(); context.ExecuteQueryWithTrace(); }
/// <summary> /// Promotes a model event outside of the model handler. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> protected void InvokeOnModelEvent(object sender, ModelEventArgs args) { TraceService.VerboseFormat((int)LogEventId.CoreCalls, "Entering InvokeOnModelEvent with sender: [{0}] and args: [{1}]", new object[] { sender, args }); if (OnModelEvent != null) { TraceService.VerboseFormat((int)LogEventId.CoreCalls, "OnModelEvent is not NULL. Calling OnModelEvent with sender: [{0}] and args: [{1}]", new object[] { sender, args }); OnModelEvent.Invoke(this, args); } else { TraceService.Verbose((int)LogEventId.CoreCalls, "OnModelEvent is NULL. Skipping."); } TraceService.VerboseFormat((int)LogEventId.CoreCalls, "Leaving InvokeOnModelEvent with sender: [{0}] and args: [{1}]", new object[] { sender, args }); }
private void DeployWelcomePage(object modelHost, DefinitionBase model, Folder folder, WelcomePageDefinition welcomePgaeModel) { var context = folder.Context; context.Load(folder); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = folder, ObjectType = typeof(Folder), ObjectDefinition = welcomePgaeModel, ModelHost = modelHost }); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Changing welcome page to: [{0}]", welcomePgaeModel.Url); folder.WelcomePage = welcomePgaeModel.Url; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = folder, ObjectType = typeof(Folder), ObjectDefinition = welcomePgaeModel, ModelHost = modelHost }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling folder.Update()"); folder.Update(); context.ExecuteQueryWithTrace(); }
protected virtual ListTemplate ResolveListTemplate(WebModelHost host, ListDefinition listModel) { var context = host.HostClientContext; var site = host.HostSite; var web = host.HostWeb; // internal names would be with '.STP', so just a little bit easier to define and find var templateName = listModel.TemplateName.ToUpper().Replace(".STP", string.Empty); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching all web.ListTemplates"); context.Load(web, tmpWeb => tmpWeb.ListTemplates); context.ExecuteQueryWithTrace(); var listTemplate = web.ListTemplates .FirstOrDefault(t => t.InternalName.ToUpper().Replace(".STP", string.Empty) == templateName); if (listTemplate == null) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Searching list template in Site.GetCustomListTemplates(web)"); var customListTemplates = site.GetCustomListTemplates(web); context.Load(customListTemplates); context.ExecuteQueryWithTrace(); listTemplate = customListTemplates .FirstOrDefault(t => t.InternalName.ToUpper().Replace(".STP", string.Empty) == templateName); } if (listTemplate == null) { throw new SPMeta2Exception(string.Format("Can't find custom list template with internal Name:[{0}]", listModel.TemplateName)); } return(listTemplate); }
protected override void ProcessFieldProperties(Field field, FieldDefinition fieldModel) { // let base setting be setup base.ProcessFieldProperties(field, fieldModel); var typedFieldModel = fieldModel.WithAssertAndCast <CalculatedFieldDefinition>("model", value => value.RequireNotNull()); var typedField = field.Context.CastTo <FieldCalculated>(field); if (!string.IsNullOrEmpty(typedFieldModel.Formula)) { // can't really validate it automatically // Improve CalculatedFieldDefinition with field ref check // https://github.com/SubPointSolutions/spmeta2/issues/648 TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating formula for a CalculatedField. Ensure FieldReferences are correct."); typedField.Formula = typedFieldModel.Formula; } if (!string.IsNullOrEmpty(typedFieldModel.OutputType)) { typedField.OutputType = (FieldType)Enum.Parse(typeof(FieldType), typedFieldModel.OutputType); } }
protected virtual FieldLookup GetDependentLookupField(FieldCollection fields, DependentLookupFieldDefinition definition) { var context = fields.Context; Field field = null; var scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { fields.GetByInternalNameOrTitle(definition.InternalName); } using (scope.StartCatch()) { } } TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "ExecuteQuery()"); context.ExecuteQueryWithTrace(); if (!scope.HasException) { field = fields.GetByInternalNameOrTitle(definition.InternalName); context.Load(field); context.ExecuteQueryWithTrace(); return(context.CastTo <FieldLookup>(field)); } return(null); }