Nintex Workflow · PowerShell · SharePoint · SharePoint 2013 · Workflow

SharePoint 2013 – Start/Cancel/Restart workflows from PowerShell


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)

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

6 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

    Like

  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.

    Like

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s