Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

3 Answers

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();
}
share|improve this answer
    
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 Dec 5 '12 at 17:04
    
Exception generated from your code: Exception from HRESULT: 0x8102009B .. Any suggestion? –  Ziba Leah Dec 5 '12 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. –  Bruce Dec 5 '12 at 19:47
    
Mmm but I'm System Account, admin running code in SPSecurity.RunWithElevatedPrivileges.. –  Ziba Leah Dec 6 '12 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. –  Bruce Dec 7 '12 at 2:31
add comment
up vote 0 down vote accepted

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);
    }
share|improve this answer
    
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 May 28 '13 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 May 28 '13 at 12:30
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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