Monday, December 15, 2014

Running SharePoint List Workflows on a particular view of List Items using PowerShell

Running SharePoint List Workflows on a particular view of List Items using PowerShell
Too often it is required to execute workflows on multiple list items on a particular list view and there is no OOTB feature available in SharePoint to achieve this objective. One such example is to update all list items, delete multiple list items based on a criteria set in Workflow etc.
Below Power Shell script will help you to run List Workflow on all list items in one go.

# This Script is for running on all items of a particular List View
# URL of the Site
$web = Get-SPWeb -Identity "http://xxxxxxxxxx/"
$manager = $web.Site.WorkFlowManager
# Name of the list
$list = $web.Lists["ListName"]
$ListView = $list.Views["ViewName"]
# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("WorkflowName","en-US")
$data = $assoc.AssociationData
$items = $list.GetItems($ListView) 
foreach($item in $items)
 {
# Putting five seconds delay because if power shell sends all instances to server workflows get hanged.
start-sleep -seconds 5
 $wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
 }
$manager.Dispose()
$web.Dispose()

Copy the script save it anywhere in your machine such as C Drive with extension .ps1.
Run the SharePoint Management Shell as “Run As Administrator”.
Type & “path where .ps1 is saved”.
And here we go.
Good Luck.



No comments:

Post a Comment