Wednesday, September 5, 2018

Start a workflow with Powershell - and pause for each item

This is one I use often.  It's pretty simple, but it allows me to start a workflow for all items in a list, and to wait between each item.  SharePoint is rickety - and delaying a kickoff of 400 workflows on 400 items is probably a good idea.  :)

Add-PSSnapin microsoft.sharepoint.powershell
# URL of the Site
$web = Get-SPWeb -Identity "http://somesharepointsite.com/sites/collection/subweb"
#Get workflow manager
$manager = $web.Site.WorkFlowManager
# Name of the list
$list = $web.Lists["Some SharePoint List"]
# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("Some Workflow","en-US")
$count = 0
$data = $assoc.AssociationData
$items = $list.Items
foreach($item in $items)
 {
    #Pause one minute between each workflow
    Start-Sleep -s 60
   
    #Fire the workflow for that item
    $wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
 
    #Write to console that the workflow has fired
    write-host "Workflow fired for item: " $item.Name
 }
$manager.Dispose()
$web.Dispose()