Cancelling The SharePoint Online Workflows Using CSOM

Introduction

In this blog, you will learn, how to cancel Workflows of List Item/Document in List/Library, using CSOM with PowerShell Script for any SharePoint platform site.

Pre-process

Following prerequisites are required to create Workflow in SharePoint lists/libraries before cancelling Workflow:

  • Install SharePoint Designer 2013.
  • Open SharePoint Designer 2013 and connect to a SharePoint 2013 site.
  • Create a new List workflow based on the SharePoint 2013 Workflow platform.
  • Click Workflows node in the Navigation pane.

    • Click the List Workflow drop-down in the New section of the ribbon.

      Workflow

  • Select the list you want to associate with the new Workflow.
  • On the Create List Workflow dialog box, enter a name and description for Workflow and make sure, the Platform Type is set to SharePoint 2013 Workflow.

    Create List Workflow

  • Click OK to create Workflow.
  • Now, Workflow is created. You can add Actions, Conditions, Stages, Steps and Loops to build your Workflow. These Workflow components are available in the ribbon of SharePoint Designer 2013.

    components

Steps Involved to Cancel the Workflow

The following section explains the flow to cancel Workflow.

  1. Add the references, using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

    • Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
    • Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

  2. Initialize the client context object with the site URL.
    1. $siteURL = "http://siteURL.com/"   
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)   

  3. If you are trying to access SharePoint Online site, you need to setup the site credentials with the credentials parameter and set it to the client context.
    1. $userId = "ID12345"   
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString   
    3. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)   
    4. $ctx.credentials = $creds   
    5. $web= $ctx.web   

  4. If you are trying to access SharePoint On premise site, the credentials parameter is not required to be set to the context. You need to run the code on the respective SharePoint Server.

  5. Cancel Workflow of the list/library items, using the code, given below:

    • Get the List by using GetByTitle.
    • Loop through all the Items in List/Library.
    • Loop through all Workflows on each List Items/Documents.
    • Completed Workflows have to be ignored
    • Cancel Workflow, using CancelWorkflow method.
    1. try  
    2. {  
    3.   #Provide the List or Library name which contains Workflow  
    4.     $list = $ctx.web.Lists.GetByTitle("TestListWorkFlow")  
    5.     $web.AllowUnsafeUpdates = $true  
    6.     foreach($listItem in $list.Items) {  
    7.         foreach($workflow in $listItem.Workflows)  
    8.         {  
    9.           #Completed Workflows has to be ignored  
    10.             if (($listItem.Workflows | where {  
    11.                     $_.InternalState - ne "Completed"  
    12.                 }) - ne $null)  
    13.             {  
    14.               #Stop / Cancel the Workflows[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow) write - output $listItem.Title "Workflow has been stopped” -foregroundcolor green   
    15.             }  
    16.         }  
    17.     }  
    18.     $web.AllowUnsafeUpdates = $false  
    19. catch {  
    20.     write - host "$($_.Exception.Message)" - foregroundcolor red  
    21. }  

Thus, the Item Workflow has been cancelled for all the list items/document librarys.
workflow

Summary

Thus, you have learned, how to cancel SharePoint Online Workflows of List Item/Document in List/Library, using CSOM with PowerShell Script.