In some cases, we need to run workflow all items in List which is very panic work via SharePoint GUI. We must select each list item and run workflow manually which is not effective or feasible in case of too many items in list. Additionally, it is time consuming task to run workflow for each item.
Background
In some scenarios, we might need to develop workflows for existing SharePoint list which has several list items. This happens because of some enhancements or user(s) requirements. Normally, we develop a workflow for lists or libraries, and the workflow can execute manually, and an item is added or update automatically.
Scenario
However, in some cases, we need to run a workflow on all items in List which is very hard work via SharePoint GUI. We must select each list item and run workflow manually which is not effective or feasible in case of too many items in list. Additionally, it is time consuming task to run workflow for each item.
Note: workflow means SharePoint workflow which is developed via SharePoint designer.
Here, I am sharing a small piece of code in PowerShell command through which we can accomplish this requirement but note this will not start instantly those workflows. It can take up-to 5 minutes as it is triggered through SharePoint Timer Service. (use SharePoint PowerShell)
Using the PowerShell Script
$webApp = Get-SPWebApplication "http://sharepointUrl/sites/sitename"
$web = Get-SPWeb -Identity "http://sharepointUrl/sites/sitename"
$workflowmanager = $web.Site.WorkFlowManager
$list = $web.Lists["List Name"]
$assocname = $list.WorkflowAssociations.GetAssociationByName("On Item Created","en-US")
$data = $assocname.AssociationData
$items = $list.Items
foreach($item in $items)
{
$workflow = $workflowmanager.StartWorkFlow($item,$assocname,$data,$true)
}
$workflowmanager.Dispose()
$web.Dispose()
After running this PowerShell script, the workflow will be triggered for each item in the mentioned list. This small powershell script saves huge time which we do manually to run the workflows.