I have a workflow in list and i have changed something in workflow. List has already 2000 items, new modified workflow works for new item added. can someone provide me powershell command to re-run workflow on each existing items in list. As it will be very hectic to run manually for each item. Thanks
2 Answers
Try following
# URL of Site
$web = Get-SPWeb -Identity "https://yoursite/sitetest"
$manager = $web.Site.WorkFlowManager
# Name of the list
$list = $web.Lists["Shared Documents"]
# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("On Item Created","en-US")
$data = $assoc.AssociationData
$items = $list.Items
foreach($item in $items)
{
$wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
}
$manager.Dispose()
$web.Dispose()
#
More detail: http://www.thesysadminhimself.com/2013/09/sharepoint-start-workflow-all-items-powershell.html
-
1Script gives an error "Exception calling "StartWorkflow" with "4" argument(s): "Value cannot be null."Nina G– Nina G2016-09-25 01:34:22 +00:00Commented Sep 25, 2016 at 1:34
-
@Aspire27 Are you using SharePoint 2010 or 2013 workflow? $list.WorkflowAssociations in this answer will return only SharePoint 2010 workflows associated with that list.Cthulhubutt– Cthulhubutt2016-10-06 15:43:50 +00:00Commented Oct 6, 2016 at 15:43
I was able to do so using the script below:
$sourceWebURL = '<URL>'
$sourceListName = '<List Name>'
$TargetWorkflow = '<Workflow Name>'
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$items = $spSourceList.getItems()
#-- Getting a Workflow manager object to work with.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
#-- Getting the subscriptions
$sub = $wfm.GetWorkflowSubscriptionService()
#-- Getting the specific workflow within the list of subscriptions on the specific list. (SP2010 associated workflows basically)
$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq "$TargetWorkflow"}
#-- Getting a Workflow instance in order to perform my commands.
$wfis=$wfm.GetWorkflowInstanceService()
Foreach($item in $items){
#-- Creating the dictionary object I need to parse into StartWorkflow. This could be most other workflow commands.
$object = New-Object 'system.collections.generic.dictionary[string,object]'
$object.Add("WorkflowStart", "StartWorkflow");
$wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
}
Reference is below: