1

I have a workflow linked to my list. Is there any programmatically way to select a list item and restart the associated workflow?

Something like:

SPListItem item = List.Items.GetById(1);
item.Workflows[GUID].Restart();

Is there any other way to get the same result?

Thank you very much!

3 Answers 3

6

SharePoint does not provide a method of restarting a workflow. You can however cancel a running workflow and start it again. A good example of canceling a running workflow can be found here: http://blog.brianfarnhill.com/2008/10/how-to-cancel-a-workflow-programmatically

The snippit of code below will start your workflow.

private static void StartWorkflow(SPListItem listItem, string workflowName)
{
    // Get the workflow by name that's associated with the list item
    SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations.GetAssociationByName(workflowName, System.Globalization.CultureInfo.CurrentCulture);

    // Start the workflow
    listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData, true);
    listItem.Update();
}
5
  • Hi Bruce, could you add an explanation of your code please, just to make it a complete answer. Also please read the FAQ for a guide on how to answer questions. Working together we can make this a better place :)
    – Hugh Wood
    Commented Dec 5, 2012 at 17:04
  • Exception generated from your code: Exception from HRESULT: 0x8102009B .. Any suggestion?
    – Ziba Leah
    Commented Dec 5, 2012 at 17:17
  • The exception you are receiving appears to be the result of a permissions issue. The account your code is running under may not have permissions to the list item.
    – SF1Dev
    Commented Dec 5, 2012 at 19:47
  • Mmm but I'm System Account, admin running code in SPSecurity.RunWithElevatedPrivileges..
    – Ziba Leah
    Commented Dec 6, 2012 at 12:05
  • If you're still receiving the error while running with elevated privileges, check that your reference to the web object was created within your elevated privileges block.
    – SF1Dev
    Commented Dec 7, 2012 at 2:31
1

The problem of the Bruce answer is that if the workflow is already started if throws an exption..

To fix this I modified his code, checking and removing the Workflow if there is!

   private static void RestartWorkflow(SPListItem listItem, string workflowName)
    {
        SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations.GetAssociationByName(workflowName, System.Globalization.CultureInfo.CurrentCulture); 
        var WF = listItem.Workflows.Cast<SPWorkflow>().Where(X => X.ParentAssociation.Name == "Flusso di gestione delle informazioni"); //My wf name
        if (WF.Any()) //if there is remove
        {
            listItem.Web.Site.WorkflowManager.RemoveWorkflowFromListItem(WF.First());
        } //start
        listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData, true);
    }
2
  • Are you all sure? You're passing in wfAssoc.AssociationData as eventData? wfAssoc is actually association data. Any way to get the Initiation data to re-send it to workflow?
    – Gomiunik
    Commented May 28, 2013 at 8:37
  • The only thing I know is that the code I posted works for a couple of custom workflow I written.. I dont' know if there are more specific way to do that.. If there are please post an anwser so I can accept a better one!
    – Ziba Leah
    Commented May 28, 2013 at 12:30
0

If you use reusable Workflow. Change "...listItem.ParentList.WorkflowAssociations..." to "...listItem.ContentType.WorkflowAssociations..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.