From 7c18ecfd86082e5c1cf3629c84b26c902992ca97 Mon Sep 17 00:00:00 2001 From: SubPointSupport Date: Mon, 20 Mar 2017 06:53:45 -0700 Subject: [PATCH] + .AddWorkflowAssociation() should support list scoped content types #980 + CSOM support --- .../ContentTypeLinkModelHandler.cs | 32 +++++++++------- .../WorkflowAssociationModelHandler.cs | 37 +++++++++++++++++++ .../ModelHosts/ContentTypeLinkModelHost.cs | 24 ++++++++++++ .../SPMeta2.CSOM/ModelHosts/ListModelHost.cs | 2 +- SPMeta2/SPMeta2.CSOM/SPMeta2.CSOM.csproj | 1 + .../WorkflowAssociationScenariosTest.cs | 1 + 6 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 SPMeta2/SPMeta2.CSOM/ModelHosts/ContentTypeLinkModelHost.cs diff --git a/SPMeta2/SPMeta2.CSOM/ModelHandlers/ContentTypeLinkModelHandler.cs b/SPMeta2/SPMeta2.CSOM/ModelHandlers/ContentTypeLinkModelHandler.cs index f0cd1ac7f..1983ec965 100644 --- a/SPMeta2/SPMeta2.CSOM/ModelHandlers/ContentTypeLinkModelHandler.cs +++ b/SPMeta2/SPMeta2.CSOM/ModelHandlers/ContentTypeLinkModelHandler.cs @@ -8,7 +8,8 @@ using SPMeta2.ModelHandlers; using SPMeta2.Services; using SPMeta2.Utils; -using SPMeta2.CSOM.ModelHosts; +using SPMeta2.CSOM.ModelHosts; +using SPMeta2.ModelHosts; namespace SPMeta2.CSOM.ModelHandlers { @@ -20,8 +21,8 @@ public override Type TargetType } public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext) - { - var modelHost = modelHostContext.ModelHost; + { + var modelHost = modelHostContext.ModelHost as CSOMModelHostBase; var model = modelHostContext.Model; var childModelType = modelHostContext.ChildModelType; var action = modelHostContext.Action; @@ -33,18 +34,21 @@ public override void WithResolvingModelHost(ModelHostResolveContext modelHostCon var context = list.Context; context.Load(list, l => l.ContentTypes); - context.ExecuteQueryWithTrace(); - - var listContentType = FindListContentType(list, contentTypeLinkModel); - - action(new ModelHostContext - { - Site = listModelHost.HostSite, - Web = listModelHost.HostWeb, - ContentType = listContentType - }); + context.ExecuteQueryWithTrace(); + + var contentType = FindListContentType(list, contentTypeLinkModel); + + var contentTypeLinkHost = ModelHostBase.Inherit(modelHost, host => + { + host.HostContentType = contentType; + host.HostList = list; + }); + + action(contentTypeLinkHost); + + if (!contentTypeLinkHost.ShouldUpdateHost) + contentType.Update(false); - listContentType.Update(false); context.ExecuteQueryWithTrace(); } diff --git a/SPMeta2/SPMeta2.CSOM/ModelHandlers/WorkflowAssociationModelHandler.cs b/SPMeta2/SPMeta2.CSOM/ModelHandlers/WorkflowAssociationModelHandler.cs index 4a43df67c..7dee7edce 100644 --- a/SPMeta2/SPMeta2.CSOM/ModelHandlers/WorkflowAssociationModelHandler.cs +++ b/SPMeta2/SPMeta2.CSOM/ModelHandlers/WorkflowAssociationModelHandler.cs @@ -53,6 +53,22 @@ public override void DeployModel(object modelHost, DefinitionBase model) DeployContentTypeWorkflowAssociationDefinition(modelHost, contentType, workflowAssociationModel); } + else if (modelHost is ContentTypeLinkModelHost) + { + var contentTypeLinkModelHost = (modelHost as ContentTypeLinkModelHost); + + // don't update content type link within list + if (contentTypeLinkModelHost.HostList != null) + contentTypeLinkModelHost.ShouldUpdateHost = false; + + var contentType = contentTypeLinkModelHost.HostContentType; + var web = contentTypeLinkModelHost.HostWeb; + + if (contentTypeLinkModelHost.HostList != null) + contentTypeLinkModelHost.ShouldUpdateHost = false; + + DeployContentTypeWorkflowAssociationDefinition(modelHost, contentType, workflowAssociationModel); + } else { throw new SPMeta2NotSupportedException("model host should be of type ListModelHost or WebModelHost"); @@ -77,6 +93,14 @@ private Web GetWebFromModelHost(object modelHost) return (modelHost as ModelHostContext).Web; } + if (modelHost is ContentTypeLinkModelHost) + { + var listContentTypeHost = (modelHost as ContentTypeLinkModelHost); + + + return listContentTypeHost.HostWeb; + } + throw new SPMeta2NotSupportedException("model host should be of type ListModelHost or WebModelHost"); } @@ -121,6 +145,19 @@ protected WorkflowAssociation FindExistringWorkflowAssotiation(object modelHost, return res.FirstOrDefault(); } + if (modelHost is ContentTypeLinkModelHost) + { + var list = (modelHost as ContentTypeLinkModelHost).HostList; + var context = list.Context; + + var defName = def.Name; + + var res = context.LoadQuery(list.WorkflowAssociations.Where(w => w.Name == defName)); + context.ExecuteQueryWithTrace(); + + return res.FirstOrDefault(); + } + throw new SPMeta2NotSupportedException("model host should be of type ListModelHost or WebModelHost"); } diff --git a/SPMeta2/SPMeta2.CSOM/ModelHosts/ContentTypeLinkModelHost.cs b/SPMeta2/SPMeta2.CSOM/ModelHosts/ContentTypeLinkModelHost.cs new file mode 100644 index 000000000..e2cea1c1f --- /dev/null +++ b/SPMeta2/SPMeta2.CSOM/ModelHosts/ContentTypeLinkModelHost.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Microsoft.SharePoint; +using Microsoft.SharePoint.Client; + +namespace SPMeta2.CSOM.ModelHosts +{ + public class ContentTypeLinkModelHost : ListModelHost + { + #region constructors + + + #endregion + + #region properties + + public ContentType HostContentType { get; set; } + + #endregion + } +} diff --git a/SPMeta2/SPMeta2.CSOM/ModelHosts/ListModelHost.cs b/SPMeta2/SPMeta2.CSOM/ModelHosts/ListModelHost.cs index 758d32d0e..d49f43974 100644 --- a/SPMeta2/SPMeta2.CSOM/ModelHosts/ListModelHost.cs +++ b/SPMeta2/SPMeta2.CSOM/ModelHosts/ListModelHost.cs @@ -2,7 +2,7 @@ namespace SPMeta2.CSOM.ModelHosts { - public class ListModelHost : CSOMModelHostBase + public class ListModelHost : WebModelHost { #region properties diff --git a/SPMeta2/SPMeta2.CSOM/SPMeta2.CSOM.csproj b/SPMeta2/SPMeta2.CSOM/SPMeta2.CSOM.csproj index 65a800580..08389eca2 100644 --- a/SPMeta2/SPMeta2.CSOM/SPMeta2.CSOM.csproj +++ b/SPMeta2/SPMeta2.CSOM/SPMeta2.CSOM.csproj @@ -257,6 +257,7 @@ + diff --git a/SPMeta2/SPMeta2.Regression.Tests/Impl/Scenarios/WorkflowAssociationScenariosTest.cs b/SPMeta2/SPMeta2.Regression.Tests/Impl/Scenarios/WorkflowAssociationScenariosTest.cs index 7222c135b..25f0464b0 100644 --- a/SPMeta2/SPMeta2.Regression.Tests/Impl/Scenarios/WorkflowAssociationScenariosTest.cs +++ b/SPMeta2/SPMeta2.Regression.Tests/Impl/Scenarios/WorkflowAssociationScenariosTest.cs @@ -172,6 +172,7 @@ public void CanDeploy_WorkflowAssociation_UnderContentTypeLink() { def.Hidden = true; def.TemplateType = BuiltInListTemplateTypeId.Tasks; + def.ContentTypesEnabled = true; }); var historyList = ModelGeneratorService.GetRandomDefinition(def =>