本文讲述使用VS2012 开发SharePoint 2013 客户化的action (代码)。
使用代码 客户化的action的action好处是所有本地代码可以做的事情都可以做,相对声明式的action 更灵活和强大,但是Office 365和SharePoint online是不支持的,只能部署在on-promise 环境。
下面以一个简单需求为例来讲述如何使用代码开发SharePoint 2013 客户化的action:
需求:开发一个action反转一个字符,输入要反转的字符串,输出反转结果
准备工作同:http://blog.csdn.net/abrahamcheng/article/details/12652495
使用VS 2012完成以下工作:
1. 新建一个Workflow library 的项目,命名为CustomCodeActions
2. 在项目中新建 code activity Item
3. ReverseString.cs 内容为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
namespace CustomCodeActions
{
public sealed class ReverseSretring : CodeActivity
{
// Define an activity input argument of type string
[RequiredArgument]
public InArgument<string> Input { get; set; } //输入参数
[RequiredArgument]
public OutArgument<string> Output { get; set; } // 输出参数
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
string input = Input.Get<string>(context); //取得输入参数
char[] arr = input.ToCharArray();
Array.Reverse(arr);
string output = new string(arr);
Output.Set(context, output); // 设置输出参数
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<AllowedTypes>
<Assembly Name="CustomCodeActions">
<Namespace Name="CustomCodeActions">
<Type>ReverseSretring</Type>
</Namespace>
</Assembly>
</AllowedTypes>
该文件部署时要复制到
%ProgramFiles%\Workflow Manager\1.0\Workflow\Artifacts
%ProgramFiles%\Workflow Manager\1.0\Workflow\WFWebRoot\bin
5. 给项目添加强名
6.编译项目,使用VS2012 Command Prompt运行如下命令取PublickeyToken,第7步会用到它
sn -T CustomCodeActions.dll
7. 新建 ReverseString.actions4,内容为(注意替换PublickeyToken)
<?xml version="1.0" encoding="utf-8"?>
<WorkflowInfo Language="en-us">
<Actions>
<Action
Name="ReverseSretring"
ClassName="CustomCodeActions.ReverseSretring"
Assembly ="CustomCodeActions, Version=1.0.0.0, Culture=neutral, PublickeyToken=e99e4e028044a473"
Category="Custom"
AppliesTo="all">
<RuleDesigner Sentence="Reverse string %1 and out put to %2">
<!-- Define the UI widgets SharePoint designer should use for configuring this action -->
<FieldBind Field="Input" Text="Input string" Id="1" DesignerType="TextOnly" DisplayName="Input string" />
<FieldBind Field="Output" Text="Output string" Id="2" DesignerType="ParameterNames" DisplayName="Output string" />
</RuleDesigner>
<Parameters>
<Parameter Name="Input" Type="System.String, mscorlib" Direction="In" DesignerType="TextOnly" Description="Input string" />
<Parameter Name="Output" Type="System.String, mscorlib" Direction="Out" DesignerType="ParameterNames" Description="Output string" />
</Parameters>
</Action>
</Actions>
</WorkflowInfo>
部署code action:
1.在Workflow manager box上的部署
a.将CustomCodeActions.dll 和AllowedTypes.xml拷贝到如下目录
%ProgramFiles%\WorkflowManager\1.0\Workflow\Artifacts
%ProgramFiles%\WorkflowManager\1.0\Workflow\WFWebRoot\bin
b.重启"Workflow Manager backend"服务
2. 在SharePoint box服务器上的部署
a. 将 CustomCodeActions.dll注册到GAC (使用 gacutil/iCustomCodeActions.dll)
b. 将ReverseString.actions4拷贝到 ~ProgramFiles\Common Files\microsoft shared\Web ServerExtensions\15\TEMPLATE\1033\Workflow
c. 重启IIS
测试CodeAction
1. 清空SharePoint designer 2013 的缓存 C:\Users\v-abchen\AppData\Local\Microsoft\WebsiteCache
2. 启动SharePoint designer 2013,新建一个基于SharePoint 2013 平台的Site workflow
3. 给worklfow添加两个string变量
4. 将messageNeedReverse设置为123456789,并添加ReverString action
5. 输入变量设置为messageNeedReverse,输出变量设置为reversedMessage,将reversedMessage记录到workflow history 中,并添加go to "End of workflow" 结束工作流的语句
6. 启动测试工作流(不要使用系统帐号启动),等待其完成,查看workflow history: