按前面的做法,如果我们做一个审批步骤多的工作流,会发现每次都要很多createTask、onTaskChanged、completeTask等这些Actions,就如下面一样,很繁琐,每次都要设置很多属性,满屏的Action看上去也很复杂。
sharpoint 工作流是基于WF的,所以我们可以自定义Activity。
创建一个自定义Activity,起名“TaskActivity”,向里面添加createTask、onTaskChanged、completeTask和codeActivity
分别设置各自的属性,如图,如果有不明白的地方请看:
sharepoint 顺序工作流创建
现在我们来实现代码部份,先增加一个事件,用来提供给代码处理,用于指定审批人和各种属性设置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /// <summary> /// 任务事件参数 /// </summary> /// [Serializable] public class TaskEventArgs : EventArgs { /// <summary> /// 任务属性 /// </summary> public readonly SPWorkflowTaskProperties TaskProperties; public TaskEventArgs(SPWorkflowTaskProperties taskProperties) { TaskProperties = taskProperties; } } |
添加任务属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /// <summary> /// 任务属性 /// </summary> [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] [BrowsableAttribute(true)] [CategoryAttribute("Misc")] public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties TaskProperties { get { return ((Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties)(base.GetValue(ManagementSystemApproval.TaskActivity.TaskPropertiesProperty))); } set { base.SetValue(ManagementSystemApproval.TaskActivity.TaskPropertiesProperty, value); } } public static DependencyProperty TaskOutcomeProperty = DependencyProperty.Register("TaskOutcome", typeof(System.String), typeof(ManagementSystemApproval.TaskActivity)); [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] [BrowsableAttribute(true)] [CategoryAttribute("Misc")] public String TaskOutcome { get { return ((string)(base.GetValue(ManagementSystemApproval.TaskActivity.TaskOutcomeProperty))); } set { base.SetValue(ManagementSystemApproval.TaskActivity.TaskOutcomeProperty, value); } } |
加入任务事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /// <summary> /// 任务创建事件 /// </summary> [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] [BrowsableAttribute(true)] [CategoryAttribute("杂项")] public event EventHandler<TaskEventArgs> TaskCreating; /// <summary> /// 任务完成事件 /// </summary> [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] [BrowsableAttribute(true)] [CategoryAttribute("杂项")] public event EventHandler<TaskEventArgs> TaskCompleted; |
处理活动事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | private void createTask1_MethodInvoking(object sender, EventArgs e) { this.taskId = Guid.NewGuid(); if(this.TaskProperties == null ) this.TaskProperties = new SPWorkflowTaskProperties (); if (TaskCreating != null) TaskCreating(this, new TaskEventArgs(this.TaskProperties)); if (!String.IsNullOrEmpty(this.TaskProperties.AssignedTo)) this.specialPermissions.Add(this.TaskProperties.AssignedTo, SPRoleType.Contributor); } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { if (TaskCompleted != null) TaskCompleted(this, new TaskEventArgs(this.TaskProperties)); } |