Have you ever been in a situation where you have had a lot of workflows that has been started and ended up in a Error Occurred state? Where you tired of terminating them one by one before you could start the workflow again? Well I was, so I wrote a small and simple PowerShellScript for it.

What the script does is that you specify a site, list and workflow name and then all workflows will be terminated so that you can run them again. I also included the option to include already running Workflows and also a Restart parameter that could actually be used to start multiple Workflows only.

This is how it looks:

DESCRIPTION:
NAME: Cancel-SPWorkflow
Cancel SharePoint Workflows and restart them if wanted

PARAMETERS:  
-Site Url to the SharePoint Site
-List Name of the list
-Workflowname Name of the workflow
-Restart Optional if you want to restart the workflows
-IncludeRunning Optional if you want to include running workflows
-help Displays the help topic

SYNTAX:
Cancel-SPWorkflow -help
Displays the help topic for the script

Cancel-SPWorkflow -site http://yourSPSite -list Listname -Workflowname YourWorkflow
Terminates all workflows with the specific workflow name

Cancel-SPWorkflow -site http://yourSPSite -list Listname -Workflowname YourWorkflow -Includerunning
Terminates all workflows with the specific workflow name and include running workflows

Cancel-SPWorkflow -site http://yourSPSite -list Listname -Workflowname YourWorkflow -restart
Terminates all workflows with the specific workflow name and then restart the workflow for all items

The key in the script is how we cancel the workflow. That is done through the SPWorkflowManager and done like.

[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($Workflow)

And when we want to start a workflow we first need the workflow association and association data together with the SharePoint item to start it for.

$SPList.Parentweb.site.workflowmanager.startworkflow($item, $WFAssociation, $WFAssociation.AssociationData)

You find the whole script [Download not found] or from the Downloads section

Hope you find it useful



  1. Brian (Reply) on Friday 5, 2010

    When I ran this without the -restart option, it restarted all of my workflows anyway. Not only that, but it appeared to start workflows on tasks that weren’t currently started. Here’s the command I ran:

    Cancel-SPWorkflow -site [my site] -list “[my list']” -Workflowname “[my wf name]” -Includerunning

    Did I do something wrong? Or is this the way it’s supposed to work and I just misunderstood what it’s supposed to do?

  2. Ben (Reply) on Friday 5, 2010

    Question, does this script only stop and start the workflows where the message is Error Occurred, or does it start them on the whole library. I have 9000 items that I don’t want this to run on, however of those 9K, i have 700 I do need restarted, and I can’t find another solution anywhere!

    • Mattias Karlsson (Reply) on Friday 5, 2010

      Hi,

      That is the intention that is should only start for thouse that has status Error has occured. I think someone posted a comment that they had some problem with the script. Might be a bug that I need to fix. So I suggest that you test this before you go “live” with 700.

      Let me know your findings and if there is a bug I’ll try to help out fixing it.
      /Mattias

  3. Ben (Reply) on Friday 5, 2010

    I was not able to get this to run. Kept getting an error stating there was no workflow by the name I was typing in… brackets, no brackets, qoutes and no qoutes. Could not get the thing to fire.

    This tool is exactly what I need, just can’t get it to run.

    • Mattias Karlsson (Reply) on Friday 5, 2010

      Hi,

      That sounds a bit strange I have to say. What’s the name of the workflow? Could it be that you have changed the name of the workflow. It might be (need to look into that) that it’s only the display-name that is changed when you change the name of it. The second thenk is maybe a stupid question but you are sure that you are pointing to the right site and list?

      Please let me know how it progresses.

  4. Ben (Reply) on Friday 5, 2010

    Well, I do have the right site. although I did not think that it might only be a display name. I will be giving this another go tonight. I will let you know the results.

  5. Ben (Reply) on Friday 5, 2010

    same error. “The specified workflow does not exist in the list”. here is the command I am running: ./spworkflow.ps1 Cancel-SPWorkflow -site “http://server/SiteDirectory/Accounting” -list “AP Invoices” -Workflowname “Security Modification” -restart. I get the error every time no matter how I run it. This workflow was always called Security Modification. Granted I have previous versions, the name has always been the same. Any thoughts?