2017-03-06 61 views
0

目前正在使用下面的C#代码启动工作流启动工作流通过使用用户帐户

private static void StartWorkflow(SPListItem listItem) 
     { 
      SPSecurity.RunWithElevatedPrivileges(delegate() 
      { 
       using (SPSite elevatedSite = new SPSite(listItem.Web.Site.ID)) 
       { 
        using (SPWeb elevatedWeb = elevatedSite.OpenWeb(listItem.Web.ID)) 
        { 
         elevatedWeb.AllowUnsafeUpdates = true; 
         SPWorkflowManager manager = elevatedSite.WorkflowManager; 
         SPListItem elevatedListItem = elevatedWeb.Lists.TryGetList(listItem.ParentList.Title).GetItemById(listItem.ID); 
         foreach (SPWorkflow workflow in manager.GetItemWorkflows(elevatedListItem)) 
         { 
          SPWorkflowAssociation wfAssoc = elevatedListItem.ParentList.WorkflowAssociations.GetAssociationByName(workflow.ParentAssociation.Name, System.Globalization.CultureInfo.CurrentCulture); 
          elevatedListItem.Web.Site.WorkflowManager.StartWorkflow(elevatedListItem, wfAssoc, wfAssoc.AssociationData, true); 
          elevatedListItem.Update(); 
         } 

         // Get the workflow by name that's associated with the list item 
         elevatedWeb.AllowUnsafeUpdates = false; 
        } 
       } 
      }); 
     } 

这将使用系统帐户启动工作流。有什么办法可以使用列创建的列表项来启动工作流吗?我想使用特定用户帐户启动工作流程。

回答

0

在做了一些RND之后,我得到了问题的解决方案。我们需要将用户令牌传递给Web对象。这是我的回答

private void StartWorkflow(SPListItem item, string wfName, SPUserToken userToken) 
     { 
      using (SPSite elevatedSite = new SPSite(item.Web.Site.ID, userToken)) 
      { 
       using (SPWeb elevatedWeb = elevatedSite.OpenWeb()) 
       { 
        SPList parentList = elevatedWeb.Lists.TryGetList(item.ParentList.ToString()); 
        SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations; 
        foreach (SPWorkflowAssociation association in associationCollection) 
        { 
         if (association.Name == wfName) 
         { 
          elevatedSite.WorkflowManager.StartWorkflow(item, association, association.AssociationData); 
          break; 
         } 
        } 
       } 
      } 
     } 
相关问题