Friday 15 July 2016

Terminate a workflow for all items in a list on SharePoint Online



Today we are going to discuss about how to terminate a workflow for all items in a list on Sharepoint.

We have some pre-requisites for running this Powershell to complete this.

1. We need latest version of Powershell
2. Set-executedPolicy unrestricted

To run this we need below information:

  • Site URL where you the list is stored workflow

  • List Name, where you want to terminate running workflow

  • User Name, Permission should be there with associated user

To proceed further:
  • Start PowerShell with admin rights
  • Once run set execution policy unrestricted as we mentioned above.
EX: set-executionPolicy Unrestricted

Press enter, it will ask for y/N/A give A and enter again
Now save attached .ps1 file with required modification on drive where you can execute.
#################################################################################   
##   
## Server Health Check   
## Created by Prashant Kumar    
## Date : 14 July 2016   
## Email: prashant8590@outlook.com     
## This scripts check the server Avrg CPU and Memory utlization along with C drive   
## disk utilization and sends an email to the receipents included in the script  
################################################################################  
# Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
 
# Specify tenant admin and site URL
$SiteUrl = "https://your site/"
$ListName = "Task list"
$UserName = "user@abc@onmicrosoft.com"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString    
 
# Bind to site collection
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()
 
# Get List
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
 
$ClientContext.Load($List)
$ClientContext.ExecuteQuery()
 
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()
 
# Create WorkflowServicesManager instance
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
 
# Connect to WorkflowSubscriptionService
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
 
# Connect WorkflowInstanceService instance
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
 
$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()
 
# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()
 
# Prepare Terminate Workflow Payload
$EmptyObject = New-Object System.Object
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
 
# Loop Terminate Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){
 
  $msg = [string]::Format("Killing workflows {0} on ListItemID {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
  Write-Host $msg
 
  $itemWfInstances = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItems[$j].Id)
  $ClientContext.Load($itemWfInstances)
  $ClientContext.ExecuteQuery()
  for ($k=0;$k -lt $itemWfInstances.Count;$k++)
  {
           try {
           $WorkflowInstanceService.TerminateWorkflow($itemWfInstances[$k])
                 $msg = "Worfklow terminated on " + $ListItems[$j].Id
                 $ClientContext.ExecuteQuery()
           } catch {
                 $msg = "Error terminating workflow on " + $ListItems[$j].Id + " Details: $_"
           }
 
           Write-Host $msg
  }
}





1 comment:

  1. This worked perfectly in my O365 tennant! Thank you!

    ReplyDelete