前几天需要升级一个自定义的workflow activity,从支持SharePoint 2010的版本,升级到支持SharePoint 2013。 Branch代码出来之后,在VS工程中直接升级,之后编译,部署一切都很顺利,但是在测试的时候出现问题了。
问题是这样的:打开SharePoint Designer 2013,新建一个workflow,在workflow里使用自定义的activity,定义好了之后,保存,然后发布的时候,Designer报了一个错误:
“Errors were found when compiling the workflow. The workflow files were saved but cannot be run.” 具体的信息是“The type or namespace name xxx could not be found (are you missing a using directive or an assembly reference?)”。
我查了一下才知道(请参见点击打开链接),web.config文件中的<authorizedTypes>结构变了,在SharePoint 2013中加了一个子节点<targetFx>,因此需要将自定义的activity的authorizedType加到这个子节点中才行。
以下是2010中的配置文件和2013中的配置文件的对比:
SharePoint 2010:
<System.Workflow.ComponentModel.WorkflowCompiler> <authorizedTypes> <authorizedType Assembly="Microsoft.Office.Workflow.Actions, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"Namespace="Microsoft.Office.Workflow.Actions"TypeName="*"Authorized="True" /> <authorizedType Assembly="MyActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567"Namespace="MyActions"TypeName="*"Authorized="True" /> </authorizedTypes> <authorizedRuleTypes> <authorizedType Assembly="Microsoft.SharePoint.WorkflowActions, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"Namespace="Microsoft.SharePoint.WorkflowActions"TypeName="WorkflowCodeTypeReferenceExpression"Authorized="True" /> </authorizedRuleTypes> </System.Workflow.ComponentModel.WorkflowCompiler>SharePoint 2013:
<System.Workflow.ComponentModel.WorkflowCompiler> <authorizedTypes> <targetFx version="v4.0"> <authorizedType Assembly="Microsoft.Office.Workflow.Actions, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"Namespace="Microsoft.Office.Workflow.Actions"TypeName="*"Authorized="True" /> <authorizedType Assembly="MyActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567"Namespace="MyActions"TypeName="*"Authorized="True" /> </targetFx> </authorizedTypes> <authorizedRuleTypes> <targetFx version="v4.0"> <authorizedType Assembly="Microsoft.SharePoint.WorkflowActions, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"Namespace="Microsoft.SharePoint.WorkflowActions"TypeName="WorkflowCodeTypeReferenceExpression"Authorized="True" /> </targetFx> </authorizedRuleTypes> </System.Workflow.ComponentModel.WorkflowCompiler>
以下是正确的修改配置文件的代码:
var service = SPWebService.ContentService; var addModification = new SPWebConfigModification(); addModification.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes/targetFx"; addModification.Name= "authorizedType[@Assembly='MyActions'][@Namespace='MyActions'][@TypeName='*'][@Authorized='True']"; addModification.Owner= "MyActions"; addModification.Sequence= 0; addModification.Type= SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; addModification.Value= @"<authorizedType Assembly=""MyActions""Namespace=""MyActions"" TypeName=""*""Authorized=""True"" />"; service.WebConfigModifications.Add(addModification); service.Update(); service.ApplyWebConfigModifications();