protected Folder ExtractFolderFromModelHost(object modelHost) { if (modelHost is WebModelHost) { return((modelHost as WebModelHost).HostWeb.RootFolder); } else if (modelHost is ListModelHost) { return((modelHost as ListModelHost).HostList.RootFolder); } else if (modelHost is FolderModelHost) { var folderModelHost = (modelHost as FolderModelHost); if (folderModelHost.CurrentListFolder != null) { return(folderModelHost.CurrentListFolder); } return(folderModelHost.CurrentListItem.Folder); } TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Unsupported model host of type: [{0}]. Throwing SPMeta2UnsupportedModelHostException", new[] { modelHost }); throw new SPMeta2UnsupportedModelHostException("modelHost should be web/list/folder model host"); }
protected WorkflowDefinition GetWorkflowDefinition(object host, ClientContext hostclientContext, Web web, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving workflow definition by DisplayName: [{0}]", workflowSubscriptionModel.WorkflowDisplayName); var context = hostclientContext; //var web = list.ParentWeb; var workflowServiceManager = new WorkflowServicesManager(hostclientContext, web); //context.Load(web); //context.Load(list); context.ExecuteQueryWithTrace(); hostclientContext.Load(workflowServiceManager); hostclientContext.ExecuteQueryWithTrace(); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); hostclientContext.Load(workflowSubscriptionService); hostclientContext.Load(workflowDeploymentService); hostclientContext.Load(tgtwis); hostclientContext.ExecuteQueryWithTrace(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); hostclientContext.Load(publishedWorkflows); hostclientContext.ExecuteQueryWithTrace(); var result = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (result == null) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find workflow definition with DisplayName: [{0}]. Provision might break.", workflowSubscriptionModel.WorkflowDisplayName); } return(result); }
protected Field FindField(object modelHost, FieldDefinition definition) { if (modelHost is SiteModelHost) { return(FindExistingSiteField(modelHost as SiteModelHost, definition)); } if (modelHost is ListModelHost) { return(FindExistingListField((modelHost as ListModelHost).HostList, definition)); } TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "FindField() does not support modelHost of type: [{0}]. Throwing SPMeta2NotSupportedException", modelHost); throw new SPMeta2NotSupportedException( string.Format("Validation for artifact of type [{0}] under model host [{1}] is not supported.", definition.GetType(), modelHost.GetType())); }
private Field FindExistingField(Web web, Guid id) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Finding site field by ID: [{0}]", id); var context = web.Context; var scope = new ExceptionHandlingScope(context); Field field = null; using (scope.StartScope()) { using (scope.StartTry()) { web.AvailableFields.GetById(id); } using (scope.StartCatch()) { } } context.ExecuteQueryWithTrace(); if (!scope.HasException) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Found site field by ID: [{0}]", id); field = web.AvailableFields.GetById(id); context.Load(field); context.ExecuteQueryWithTrace(); } else { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site field by ID: [{0}]. Provision might fatally fail.", id); } return(field); }
protected WorkflowDefinition GetWorkflowDefinition(object host, SPWeb web, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving workflow definition by DisplayName: [{0}]", workflowSubscriptionModel.WorkflowDisplayName); var workflowServiceManager = new WorkflowServicesManager(web); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); var result = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (result == null) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find workflow definition with DisplayName: [{0}]. Provision might break.", workflowSubscriptionModel.WorkflowDisplayName); } return(result); }
protected Folder ExtractFolderFromModelHost(object modelHost) { if (modelHost is WebModelHost) { return((modelHost as WebModelHost).HostWeb.RootFolder); } else if (modelHost is ListModelHost) { return((modelHost as ListModelHost).HostList.RootFolder); } else if (modelHost is FolderModelHost) { #if NET35 throw new SPMeta2NotImplementedException("Not implemented for SP2010 - https://github.com/SubPointSolutions/spmeta2/issues/770"); #endif #if !NET35 var folderModelHost = (modelHost as FolderModelHost); if (folderModelHost.CurrentListFolder != null) { return(folderModelHost.CurrentListFolder); } return(folderModelHost.CurrentListItem.Folder); #endif } TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Unsupported model host of type: [{0}]. Throwing SPMeta2UnsupportedModelHostException", new[] { modelHost }); throw new SPMeta2UnsupportedModelHostException("modelHost should be web/list/folder model host"); }
private void DeployListFieldLink(object modelHost, List list, ListFieldLinkDefinition listFieldLinkModel) { var web = list.ParentWeb; var context = list.Context; var fields = list.Fields; context.Load(fields); context.ExecuteQueryWithTrace(); Field existingListField = null; Field tmp = null; try { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Fetching site field by ID: [{0}]", listFieldLinkModel.FieldId); tmp = list.Fields.GetById(listFieldLinkModel.FieldId); context.ExecuteQueryWithTrace(); } catch (Exception exception) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site field by ID: [{0}]. Provision might break.", listFieldLinkModel.FieldId); } finally { if (tmp != null && tmp.ServerObjectIsNull.HasValue && !tmp.ServerObjectIsNull.Value) { existingListField = tmp; } } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = existingListField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); if (existingListField == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list field"); //var avialableField = web.AvailableFields; //context.Load(avialableField); //context.ExecuteQueryWithTrace(); var siteField = FindExistingSiteField(web, listFieldLinkModel.FieldId); fields.Add(siteField); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = siteField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list field"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingListField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var listModelHost = modelHost.WithAssertAndCast <ListModelHost>("modelHost", value => value.RequireNotNull()); var contentTypeLinkModel = model.WithAssertAndCast <ContentTypeLinkDefinition>("model", value => value.RequireNotNull()); var list = listModelHost.HostList; if (list.ContentTypesEnabled) { var web = list.ParentWeb; SPContentType targetContentType = null; // load by id, then fallback on name if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeId)) { var contentTypeId = new SPContentTypeId(contentTypeLinkModel.ContentTypeId); targetContentType = web.AvailableContentTypes[contentTypeId]; } if (targetContentType == null && !string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeName)) { targetContentType = web.AvailableContentTypes[contentTypeLinkModel.ContentTypeName]; } if (targetContentType == null) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site content type by ID: [{0}] or Name:[{1}].", new object[] { contentTypeLinkModel.ContentTypeId, contentTypeLinkModel.ContentTypeName }); throw new SPMeta2Exception(string.Format("Cannot find site content type by ID: [{0}] or Name:[{1}].", new object[] { contentTypeLinkModel.ContentTypeId, contentTypeLinkModel.ContentTypeName })); } var currentListContentType = GetListContentType(list, contentTypeLinkModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentListContentType, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); if (currentListContentType == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list content type link"); var listCt = list.ContentTypes.Add(targetContentType); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = listCt, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); //list.Update(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list content type link"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentListContentType, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); } } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "ContentTypesEnabled is FALSE. Provision might break."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var listModelHost = modelHost.WithAssertAndCast <ListModelHost>("modelHost", value => value.RequireNotNull()); var contentTypeLinkModel = model.WithAssertAndCast <ContentTypeLinkDefinition>("model", value => value.RequireNotNull()); var list = listModelHost.HostList; var context = list.Context; context.Load(list, l => l.ContentTypesEnabled); context.ExecuteQueryWithTrace(); if (list.ContentTypesEnabled) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "ContentTypesEnabled is TRUE. Processing content type link"); var web = list.ParentWeb; // context.Load(web, w => w.AvailableContentTypes); //context.Load(list, l => l.ContentTypes); context.Load(list, l => l.ContentTypes.Include( ct => ct.Id, ct => ct.Name, ct => ct.ReadOnly, ct => ct.Parent.Id)); context.ExecuteQueryWithTrace(); // load by id, then fallback on name ContentType targetContentType = null; if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeId)) { targetContentType = web.AvailableContentTypes.GetById(contentTypeLinkModel.ContentTypeId); context.Load(targetContentType); context.ExecuteQueryWithTrace(); } if (targetContentType == null && !string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeName)) { var name = contentTypeLinkModel.ContentTypeName; context.Load(web.AvailableContentTypes, c => c.Where(w => w.Name == name)); context.ExecuteQueryWithTrace(); targetContentType = web.AvailableContentTypes[0]; } if (targetContentType == null || targetContentType.ServerObjectIsNull == true) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site content type by ID: [{0}] or Name:[{1}].", new object[] { contentTypeLinkModel.ContentTypeId, contentTypeLinkModel.ContentTypeName }); throw new SPMeta2Exception(string.Format("Cannot find site content type by ID: [{0}] or Name:[{1}].", new object[] { contentTypeLinkModel.ContentTypeId, contentTypeLinkModel.ContentTypeName })); } var listContentType = FindListContentType(list, contentTypeLinkModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = listContentType, ObjectType = typeof(ContentType), ObjectDefinition = model, ModelHost = modelHost }); if (targetContentType != null && listContentType == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list content type link"); var ct = list.ContentTypes.Add(new ContentTypeCreationInformation { Description = targetContentType.Description, Group = targetContentType.Group, Name = targetContentType.Name, ParentContentType = targetContentType, }); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = ct, ObjectType = typeof(ContentType), ObjectDefinition = model, ModelHost = modelHost }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "list.Update()"); list.Update(); context.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list content type link"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = listContentType, ObjectType = typeof(ContentType), ObjectDefinition = model, ModelHost = modelHost }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "listContentType.Update(false)"); // no update is required for content type link // besides, CTH wouldn't work // .AddContentTypeLink() should work well with the read-only content types (Content Type Hub) // https://github.com/SubPointSolutions/spmeta2/issues/745 // listContentType.Update(false); // context.ExecuteQueryWithTrace(); } } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "ContentTypesEnabled is FALSE. Provision might break."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var listModelHost = modelHost.WithAssertAndCast <ListModelHost>("modelHost", value => value.RequireNotNull()); var contentTypeLinkModel = model.WithAssertAndCast <ContentTypeLinkDefinition>("model", value => value.RequireNotNull()); var list = listModelHost.HostList; if (!list.ContentTypesEnabled) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "List [{0}] does not allow content types. Throwing SPMeta2Exception.", list.RootFolder.ServerRelativeUrl); throw new SPMeta2Exception(string.Format("List [{0}] does not allow content types.", list.RootFolder.ServerRelativeUrl)); } var rootWeb = list.ParentWeb.Site.RootWeb; var contentTypeId = new SPContentTypeId(contentTypeLinkModel.ContentTypeId); var targetContentType = rootWeb.ContentTypes[contentTypeId]; if (targetContentType == null) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site content type by ID: [{0}]. Throwing SPMeta2Exception.", contentTypeId); throw new SPMeta2Exception(string.Format("Cannot find site content type with ID [{0}].", contentTypeId)); } var currentListContentType = GetListContentType(list, contentTypeLinkModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentListContentType, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); if (currentListContentType == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list content type link"); var listCt = list.ContentTypes.Add(targetContentType); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = listCt, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); //list.Update(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list content type link"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentListContentType, ObjectType = typeof(SPContentType), ObjectDefinition = contentTypeLinkModel, ModelHost = modelHost }); } }
private void DeployListFieldLink(object modelHost, List list, ListFieldLinkDefinition listFieldLinkModel) { var web = list.ParentWeb; var context = list.Context; var fields = list.Fields; context.Load(fields); context.ExecuteQueryWithTrace(); Field existingListField = null; Field tmp = null; try { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Fetching site field by ID: [{0}]", listFieldLinkModel.FieldId); tmp = list.Fields.GetById(listFieldLinkModel.FieldId); context.ExecuteQueryWithTrace(); } catch (Exception exception) { TraceService.ErrorFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find site field by ID: [{0}]. Provision might break.", listFieldLinkModel.FieldId); } finally { if (tmp != null && tmp.ServerObjectIsNull.HasValue && !tmp.ServerObjectIsNull.Value) { existingListField = tmp; } } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = existingListField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); if (existingListField == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new list field"); //var avialableField = web.AvailableFields; //context.Load(avialableField); //context.ExecuteQueryWithTrace(); var siteField = FindExistingSiteField(web, listFieldLinkModel.FieldId); // var addFieldOptions = (AddFieldOptions)(int)listFieldLinkModel.AddFieldOptions; Field listField = null; // AddToDefaultView || !DefaultValue would use AddFieldAsXml // this would change InternalName of the field inside the list // The second case, fields.Add(siteField), does not change internal name of the field inside list if (addFieldOptions != AddFieldOptions.DefaultValue || listFieldLinkModel.AddToDefaultView) { listField = fields.AddFieldAsXml(siteField.SchemaXmlWithResourceTokens, listFieldLinkModel.AddToDefaultView, addFieldOptions); } else { listField = fields.Add(siteField); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = listField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing list field"); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingListField, ObjectType = typeof(Field), ObjectDefinition = listFieldLinkModel, ModelHost = modelHost }); } }