开发自定义的SharePoint 顺序工作流
假设有这样一个需求:
客户需要用Moss2010有个一个请假单(绑定有工作流),有这样几个用户(角色):1.Moss (员工)、2.Leader(组长)、3.Manager(经理)、4.Boss(老板)
当员工创建请假单时,启动工作流。工作流具体需求为,当员工新建了一张新的请假单时,发送邮件给Leader,请他审批,如果Leader在两天内审批(同意或拒绝)工作流结束,但如果Leader在两天内没有审批,则发送邮件给Manager请求Manager审批,如果Manager再两天内审批(同意或拒绝)工作流结束,如果Manager在两天内没有审批,则发送邮件给Boss,请求他审批该请假单,如果Boss在一天内审批(同意或拒绝)工作流结束,如果Boss在一天内审批没有审批则该单据自动生效(自动审批通过),工作流结束。
分析:考虑以上需求用MossoutOfBox 工作流和SharePointdesigner 设计的工作流都不太好实现,因此采用开发自定义的SharePoint顺序工作流。
步骤如下:
1. 启动VS2010 ->New ->Project->VisualC#->SharePoint->2010 (先择Sequential Workflow,点击OK)
选择AskForLeaving这个列表作为该工作流的绑定对象
2. VS2010 默认情况下会自动添加一个onWorkflowActivated1,在onWorkflowActivated1下面添加一个WhileActivity, 在 While Activity中添加一个delayActivity, 在WhileActivity下面添加一个 codeActivity:
3. 转到代码视图,添加如下代码 (在类中加入如下成员)
bool isWorkFlowPending = true; // 用于标记工作流对应的单据是否在等待审批 DateTime startTime = DateTime.Now; // 用于记录工作流启动的时间4. 配置 onWorkflowActivated1
a. 转到工作流审计界面
b. 选中onWorkflowActivated1,在Invoke(Handlers) 中输入OnActived 并按回车,VS会自动创建一个OnActived方法,在其中加入下列代码
this.startTime = DateTime.Now; // Send mail to Leader for request approval5. 配置delayActivity1,将其TimeoutDuration设置为00:05:00,表示该活动将会将工作流延迟5分钟再向下执行(注因为SharePoint的Timer是每五分钟处理一次工作流事件,所以延迟5分钟最长可能让InitializeTimeoutDuration中的代码10才执行一次),在delayActivity1_InitializeTimeoutDuration 中添加如下代码:
if (workflowProperties.Item["Status"].ToString().Equals("Approved") || workflowProperties.Item["Status"].ToString().Equals("Rejected")) { this.isWorkFlowPending = false; } else { DateTime now = DateTime.Now; int days = (now - this.startTime).Days; if (days == 2) { // Send mail to Manager for request approval } else if(days == 4) { // Send mail to Boss for request approval } else if (days == 6) { workflowProperties.Item["Status"] = "Approved"; workflowProperties.Item.Update(); } }
6. 配置codeActivity1,在codeActivity1_ExecuteCode 中发送邮件通知用户审批结果
// Send mail to theuser notify that record has been approved or rejected
发送邮件的代码请参考http://blog.****.net/farawayplace613/article/details/6759538
以下是完整代码Workflow1.cs:
using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Collections; using System.Drawing; using System.Linq; using System.Workflow.ComponentModel.Compiler; using System.Workflow.ComponentModel.Serialization; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Design; using System.Workflow.Runtime; using System.Workflow.Activities; using System.Workflow.Activities.Rules; using Microsoft.SharePoint; using Microsoft.SharePoint.Workflow; using Microsoft.SharePoint.WorkflowActions; namespace SequentailWFDemo.Workflow1 { public sealed partial class Workflow1 : SequentialWorkflowActivity { public Workflow1() { InitializeComponent(); } public Guid workflowId = default(System.Guid); public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); bool isWorkFlowPending = true; DateTime startTime = DateTime.Now; private void OnActived(object sender, ExternalDataEventArgs e) { this.startTime = DateTime.Now; // Send mail to Leader for request approval } private void IsWorkFlowPending(object sender, ConditionalEventArgs e) { e.Result = this.isWorkFlowPending; } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { // Send mail to the user notify that record has been approved or rejected } private void delayActivity1_InitializeTimeoutDuration(object sender, EventArgs e) { if (workflowProperties.Item["Status"].ToString().Equals("Approved") || workflowProperties.Item["Status"].ToString().Equals("Rejected")) { this.isWorkFlowPending = false; } else { DateTime now = DateTime.Now; int days = (now - this.startTime).Days; if (days == 2) { // Send mail to Manager for request approval } else if(days == 4) { // Send mail to Boss for request approval } else if (days == 6) { workflowProperties.Item["Status"] = "Approved"; workflowProperties.Item.Update(); } } } } }
Workflow1.designer.csWorkflow1.designer.cs: