Failed workflows

Using the SharePoint 2010 workflow engine, I have a list with many list items. Some of my workflows have failed and many workflows are showing an error status. This is a workflow in development so this could happen.  I now fixed my workflow and I need to restart the workflows.

Restarting workflows with PowerShell

I decided to use PowerShell to do the hard work.

So first I get my list

[code lang=text]
$web = Get-SPWeb $siteUrl
$list = $web.Lists[“MyList”]
[/code]

Get the workflow manager object:

[code lang=text]
$wfm = $web.Site.WorkFlowManager
[/code]

Getting the association and data for the worklow

[code lang=text]
$association = $list.WorkFlowAssociations |Where { $_.Name -eq “My workflow name”}
$data = $association.AssociationData
[/code]

now we’re ready to go through all the list items:

[code lang=text]
foreach ($item in $list.Items)
{
$wfsexisting = $wfm.GetItemWorkflows($item)
if ( $wfsexisting -ne $null)
{
foreach ($wfexisting in $wfsexisting)
{
if (!$wfexisting.IsCompleted -and $wfexisting.StatusText -ne “Canceled” -and $wfexisting.StatusText -ne “Failed on Start” )
{
Write-Host “Canceled ” $wfexisting.ItemName
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wfexisting)
}
}
}
Write-Host “Start workflow ” $item[“Title”]
$wf = $wfm.Startworkflow($item,$association, $data,$true)
Start-Sleep -Seconds 10
}
[/code]

When I did this I found that my nintex workflows all started, however there was a problem. The following line in the above code starts the workflow in A-Sync mode.

[code lang=text]
$wf = $wfm.Startworkflow($item,$association, $data, $true)

[/code]

No problem it means that the workflows start faster. No not really!

The workflows go first into a Starting state and while the workflows are in a starting state multiple instances are created. Looking and my log output (in the nintex workflow) I can see multiple times the same output generated.

Until the workflow has a context switch (Wait, Approval Task, State change, … and some  other activities) the multiple copies of the same instance will run. This can give many problems.

Solution: Start the workflow with a Wait Activity and the wait activity is run twice, however the activity after the wait is not. Problem solved.

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

18 thoughts on “SharePoint 2013 – Start/Cancel/Restart workflows from PowerShell”
  1. You should specifically state that this is to run ONLY SharePoint 2010 workflows, if the workflows are created as a 2013 workflow (workflow manager) then they will not show up in Workflow Association

    The process for running a 2013 workflow is quite different

    1. Hi Spanky,
      Can we restart cancelled workflows in SharePoint 2013. Do you have any power shell script for SharePoint 2013.

  2. Just to clarify, when you say “Solution=start the workflow with a Wait activity”, are you referring to the next line “Start-Sleep -Seconds 10”? Or is there some other command we need to pass to start with a “wait activity”? I think i have the same issue where the multiple copies are colliding and half (or more) of them end up in “Error Occurred” status.

  3. I’m trying to pin point a workflow on each list item, and if that particular workflow has a StatusText of “Starting” I need to cancel it, and restart it. But I’m having a hard time grabbing the StatusText for a particular workflow. Do you have any suggestions?

  4. Would the following work:

    if (!$wfexisting.IsCompleted -and $wfexisting.StatusText -eq “Starting” )

    If it doesn’t then try adding the following to display all the status values:

    if (!$wfexisting.IsCompleted )
    {
    Write-Host $wfexisting.StatusText
    }

    1. This does pull back all workflow StatusText, but not for a specific workflow. It pulls back the requested StatusText for each workflow in the item. ex. i have an item for “Lisa Lee’s Travel Vouchers” but it’s listed 3 times for 3 different workflows. Does that make sense?

  5. Ok, in the Write-Host in my previous comment can you include
    Write-Host $wfexisting.AssociationId
    If the same AssociationId is returned for each of your workflows on a single item then my guess is that your workflow is starting multiple times.

    https://social.technet.microsoft.com/Forums/sharepoint/en-US/85541784-237a-4d98-8431-6e7258a05081/workflow-running-multiple-times?forum=sharepointgenerallegacy

    How are you starting your workflows? OnChange or OnCreate or both?

    1. First, thank you for your replies.
      The one I’m targeting is started OnCreate. However, there are others in the list that are triggered OnChange. Sometime in the last week the site collection (not the farm just one site collection) stopped running workflows that were triggered automatically by OnCreate or OnChange. I can manually run them, but if auto triggered they are stuck with “Starting” as the statusText.
      So as a bandaid until I find the root issue, I’m working on finding if a particular workflow is stuck “Starting,” cancel it and rerun it. But I’m having a very hard time targeting a workflow in an item only if it’s StatusText is “Starting”, or anything for that matter 🙂

      Using your script I can see all workflows, but for some reason I can’t pin down the just the one.

  6. I’ve also seen this in other workflow solution. In general the easiest workaround is to add a wait for X minutes at the beginning of the workflow. SharePoint should then kill off the unwanted workflows without any of them having run.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.