My Problem

Using the SharePoint 2010 workflow engine, I have a list with many list items where 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.

Your Solution

I decided to get PowerShell to do the hard work.

So first I get my list

$web = Get-SPWeb $siteUrl
$list = $web.Lists["MyList"]

Get the workflow manager object:

$wfm = $web.Site.WorkFlowManager

Getting the association and data for the worklow

$association = $list.WorkFlowAssociations |Where { $_.Name -eq "My workflow name"}
$data = $association.AssociationData

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

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
}

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.

$wf = $wfm.Startworkflow($item,$association, $data, $true)</blockquote>

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.

Advertisements