事件接收器+自定义列表实现SharePoint站点集的所有工作流监视

一、新建自定义列表-工作流列表,添加栏:发起人,类型为;部门,单行文本;申请名称,超链接;流程状态;超链接

二、VS中新建事件接收器,事件接收器类型为列表工作流事件,事件源为自定义事件,勾选正在启动工作流、工作流已完成

三、代码如下:

    /// <summary>
    /// 列表工作流事件
    /// </summary>
    public class WfIntailER : SPWorkflowEventReceiver
    {
       /// <summary>
       /// 正在启动工作流.
       /// </summary>
       public override void WorkflowStarting(SPWorkflowEventProperties properties)
       {
           SPListItem wfitem = properties.ActivationProperties.Item;
           if (wfitem.Workflows.Count > 1)
           {
               properties.ErrorMessage = "不得重复启动工作流";
               properties.Cancel = true;
           }
           else
           {
               SPList list = wfitem.Web.Lists["工作流列表"];
               SPListItem item = list.AddItem();

               item["标题"] = wfitem.UniqueId.ToString();//此处Guid留作流程结束时查找自定义列表中项目

               item["发起人"] = wfitem["创建者"];
               item["部门"] = wfitem["部门"];
               item["申请名称"] = properties.ActivationProperties.WebUrl 
                   + properties.ActivationProperties.ListUrl 
                   + "/DispForm.aspx?ID=" 
                   + properties.ActivationProperties.ItemId + ", "
                   + properties.ActivationProperties.Item.Title;
               item["流程状态"] = properties.ActivationProperties.WebUrl 
                   + "/" + properties.ActivationProperties.Workflow.StatusUrl + ", 进行中";

               SPRoleDefinition RoleDefinition = wfitem.Web.RoleDefinitions.GetByType(SPRoleType.Reader);
               SPRoleAssignment RoleAssignment =
                   new SPRoleAssignment(wfitem.Web.Users.GetByID(int.Parse(item["发起人"].ToString())).LoginName, "email", "name", "notes");
               RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
               if (!item.HasUniqueRoleAssignments)
               {
                   item.BreakRoleInheritance(true);
               }
               item.RoleAssignments.Add(RoleAssignment);

               item.Update();
           }
       }

       /// <summary>
       /// 工作流已完成.
       /// </summary>
       public override void WorkflowCompleted(SPWorkflowEventProperties properties)
       {
           string WfInstanceQuery = @"<Where><Eq>
                  <FieldRef Name='WorkflowInstanceID'/><Value Type='GUID'>{0}</Value>
                  </Eq></Where>";
           using (SPSite site = new SPSite(properties.WebUrl))
           {
               using (SPWeb web = site.OpenWeb())
               {
                   //Query Workflow Task List
                   SPList wfTaskList = web.Lists["任务列表"];
                   var items = wfTaskList.GetItems(new SPQuery()
                   {
                       Query = string.Format(WfInstanceQuery, properties.InstanceId.ToString())
                   });
                   if (items.Count > 0)
                   {
                       Guid listId = new Guid(items[0]["WorkflowListId"].ToString());
                       int itemId = (int)items[0]["WorkflowItemId"];
                       string workflowField = items[0]["WorkflowName"] as string;

                       //Get the List item on which the workflow run
                       SPListItem item = web.Lists[listId].GetItemById(itemId);

                       //get the workflow status
                       int status = Convert.ToInt32(item[workflowField]);

                       //do something when approved
                       if (status == 5)
                       {
                           string targetQuey = @"<Where><Eq>
                              <FieldRef Name='LinkTitle'/><Value Type='Text'>{0}</Value>
                              </Eq></Where>";
                           var targetitems = web.Lists["工作流列表"].GetItems(new SPQuery()
                           {
                               Query = string.Format(targetQuey, item.UniqueId.ToString())
                           });
                           if (targetitems.Count > 0)
                           {
                               SPListItem targetitem = targetitems[0];
                               targetitem["流程状态"] = targetitem["流程状态"].ToString().Replace("进行中", "已完成");
                               targetitem.SystemUpdate(false);
                           }
                       }
                   }
               }
           }
       }
    }
参考文章: SharePoint 2010: Perform complex operation in the WorkflowCompleted Event Receiver



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值