In some scenario, 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 workflow for list or libraries, and workflow can execute manually, item add or update automatically.
Note: workflow means SharePoint workflow which is developed via SharePoint designer.
However, 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.
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. However, it can take up-to 5 minutes as it is triggered through SharePoint Timer Service. (use SharePoint PowerShell)
$webApp = Get-SPWebApplication "http://sharepointUrl/sites/sitename"
# URL of the Site
$web = Get-SPWeb -Identity "http://sharepointUrl/sites/sitename"
$workflowmanager = $web.Site.WorkFlowManager
# Name of the list
$list = $web.Lists["List Name"]
# Name of the Workflow
$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.