Pages

Monday, May 15, 2017

Starting a workflow in a SharePoint List using PowerShell

You can start workflow on your SharePoint list items using PowerShell with the following code:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
# URL of the Site
$web = Get-SPWeb -Identity
<Your SharePoint SIte URL>
 
$wfmanager = $web.Site.WorkFlowManager
 
#List where you would like to start the workflow
$list = $web.Lists["YOUR_LIST_NAME"]
 
# Name of the Workflow
$assoc = $list.WorkflowAssociations  |Where { $_.Name -eq "YOUR_WORKFLOW_NAME"}
Write-Host $assoc.AssociationData
 
$data = $assoc.AssociationData
$items = $list.Items

foreach($item in $items)
{
Write-Host "Processing " $item.ID

#Uncomment the following lines if you would like to start the workflow selectively. Alternatively, you can use a CAML query and rretrieve only selective items
#if ($item["Status"] -eq "Pending")
  #      {
$wf = $wfmanager.StartWorkFlow($list.GetItemById($item.ID),$assoc,$data,$true)
#}
}
#Dispose
$manager.Dispose()
$web.Dispose()

If you get the following error in PowerShell when running this script, it generallyindicates that your workflow name is not correct. You have to use your workflow name exactly as it appears in your view settings.

SharePoint 2013 – Exception calling “StartWorkflow” with “4” argument(s):

No comments:

Post a Comment