Powershell – How to start SharePoint WorkFlows in bulk

Once in a while you need to start a SharePoint  workflow for a batch of list items.

Especially when you are developing and need to to test the workflow. Or you are importing a new set of data in a list where you don’t want to run the workflow on creation of the dataset.

In these occasions you are in need of a manual start of the workflow on the full dataset or a selection of items in the list.

Solution :

This example starts a workflow on a selection of items that I control by an input file.

CLS

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

start-spassignment -global

#Open the web site, list and the workflow
$site=get-spsite <a href="http://YourSite">http://YourSite</a>
$web=get-spweb "http://YourServerName/sites/YourSiteCollection/"

#Open the library or list and workflow association
$affected = $web.getlist(<a href="http:///Lists/YourList)">http://YourServerName/Lists/YourList)

</a># WF Name
$wfname="Your WF Name here !!!" 

$manager=$site.workflowmanager
$culture=[system.globalization.cultureinfo]::currentculture

$association=$affected.workflowassociations.getassociationbyname($wfname,$culture)
$data=$association.associationdata
$wfrunoption=[microsoft.sharepoint.workflow.spworkflowrunoptions]::synchronous

#Start workflow

#Input File with all ID's where the WF should be started for.
$Customers = Get-Content "D:\Customer_IDs.txt" 

Write-host $association.ID $association.Name
Write-host ""
      
Foreach ($ID in $Customers){

    Foreach ($item in $affected.items) {
     If ($item.ID -eq $ID){
        $site.workflowmanager.startworkflow($item,$association,$data,$wfrunoption).InternalState
    }
  }
}

stop-spassignment -global

rv association, data, affected, wfrunoption, item

Enjoy!

Advertisements

Leave a Reply

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

%d bloggers like this: