Powershell – How to start SharePoint WorkFlows in bulk

January 17, 2015

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!


How to Cancel a Workflow that has not completed Successfully

October 14, 2014

Continuing on this List WF that have not been completed Successfully

Well here is the script to Cancel Workflows that have not been completed.

CLS

$web = Get-SPWeb "http://www.YourSite.com/"
$web.AllowUnsafeUpdates = $true;    

#Your List Name
$list = $web.Lists["YourListName"]
$count = 0

#Loop through all Items in List then loop through all Workflows on each List Items.         
foreach ($listItem in $list.Items) 
{
    foreach ($workflow in $listItem.Workflows) 
    {
        #Disregard Completed Workflows 
        if($Workflow.InternalState -ne "Completed" -and  $Workflow.InternalState -notlike "Cancelled*")
        {
            # Cancel Workflows        
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow)   
            Write-Host "Workflow cancelled for : " $Workflow.AssociationId " - " $Workflow.InternalState  " - "  $listitem.ID $listitem.Title $listitem.Title  
        }
    }
}
$web.AllowUnsafeUpdates = $false
$web.Dispose()

Enjoy !


How to start a SharePoint Workflow using PowerShell

October 11, 2014

Ever needed to start a workflow for multiple List Items, or a selection of items.

This Example runs a WF on Customer Items in a list.

Well here we go :

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 "www.SharePoint.com"
$web=get-spweb "www.SharePoint.com/YourSide"

#
#Open the library or list and workflow association
#
$affected = $web.getlist("http://www.SharePoint.com/Lists/Yourlist")
# WF Name
$wfname="Your Workflow" 

$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
#
#File with all ID's where the WF should be started for.
#

$Customers = Get-Content "D:\Temp\_Customers.txt" 

Foreach ($ID in $Customers){

    foreach ($item in $affected.items) {

     If ($item["ID"] -eq $ID){
      Write-Host $item["ID"] 
       $site.workflowmanager.startworkflow($item,$association,$data,$wfrunoption)
    }
  }
}

stop-spassignment -global 

I hope it worked as well for you.