Thursday, January 28, 2016

Powershell script to automatically start SharePoint workflow on a list of items.

So yesterday I needed to automatically start a 2013 workflow on a list with a lot of items on it.  Here is the Powershell script I wrote to do that.  Hope this helps someone else.

# Run Flawless Execution site workflows automatically
$web = get-spweb -identity "YOURWEBSITEURL"
$list = $web.Lists["YOURLISTNAME"]
$workflow = "YOURWORKFLOWNAME"
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
$wf = $sub.EnumerateSubscriptionsByList($list.ID) | Where-Object {$_.Name -eq "$workflow"}
$wis = $wfm.GetWorkflowInstanceService()

$items = $list.Items
foreach($item in $items)
{
$object = New-object 'system.collections.generic.dictionary[string, object]'
$object.Add("WorkflowStart", "StartWorkflow")
$wis.StartWorkFlowOnListItem($wf, $item.ID, $object)
}
$wfm.Dispose()
$web.Dispose()

No comments:

Post a Comment