none
PowerShell to Cancel SharePoint Online Workflow for Document Library RRS feed

  • Question

  • We have a SharePoint Online document library with a workflow name "TestWorkflow" and few hundred documents are running this workflow.  I need to cancel all running workflow with "TestWorkflow" column show "In Progress".  We need PowerShell to run and cancel all the workflow with above criteria.

    I found few articles using PowerShell using CSOM to cancel workflow

    1. https://www.sharepointdiary.com/2018/05/sharepoint-online-terminate-workflow-using-powershell.html

    2. http://abhilashjourney.blogspot.com/2016/10/cancel-workflow-for-particular-list.html

    3. https://en.rapidcircle.com/news/powershell-terminate-a-workflow-for-all-items-in-a-list-on-sharepoint-online

    4. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f6067af2-8cd0-42e3-a8b9-639cd38d433f/stop-all-the-running-workflows-on-a-list-using-spservices?forum=sharepointcustomizationlegacy

    dfds

    For some reasons, all above article code does not work for current SharePoint Online 2019.  My SharePoint online document library have 3 records and when the PowerShell code run, it show no workflow found and hence did not cancel any workflow where it "In Progress".

    Please help.

    Thanks

    Tee

    Sunday, January 26, 2020 5:24 AM

Answers

  • Hi,

    Did you use the SharePoint 2010 workflow?

    Try to create a new library and create a SharePoint 2013 workflow for the library, then run the PowerShell to check if it works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Mr. T1 Wednesday, January 29, 2020 7:09 PM
    Wednesday, January 29, 2020 9:51 AM
    Moderator

All replies

  • Hi Tee,

    The following PowerShell script for your reference.

    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"
    
    ##Variables for Processing
    $SiteUrl = "https://tenant.sharepoint.com/sites/team"
    $ListName="TestListWorkFlow"
    $WorkflowName="TestWorkflow"
    
    #Setup Credentials to connect
    $Cred= Get-Credential
    
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        
        #Get the Web, List and List Item Objects
        $Web=$Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
        $List = $Web.Lists.GetByTitle($ListName)
        $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
        $Ctx.Load($List)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
     
        #Initialize Workflow Manager and other related objects
        $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
        $WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
        $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
        $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
        $Ctx.Load($WorkflowAssociations)
        $Ctx.ExecuteQuery()
     
        #Get the Target workflow to Terminate
        $WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
       
        #Loop through each List Item
        ForEach($ListItem in $ListItems)
        {
            #Get Workflow Instances of the List Item           
            $WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id)
            $Ctx.Load($WorkflowInstanceCollection)
            $Ctx.ExecuteQuery()
             
            #Filter All Workflows Instances of the target workflow where Status is: Started or Canceled or Suspended
            $WorkflowInstancesToKill = $WorkflowInstanceCollection | Where { ($_.Status -eq "Started" -or $_.Status -eq "Canceled" -or $_.Status -eq "Suspended") `
                                                                     -and $_.WorkflowSubscriptionId -eq $WorkflowAssociation.Id}
             
            #Terminate Workflow Instances
            ForEach ($WorkflowInstance in $WorkflowInstancesToKill)
            {
                Try{
                    $WorkflowInstanceService.TerminateWorkflow($WorkflowInstance)
                    #To Cancel workflow, use: $WorkflowInstanceService.CancelWorkflow($WorkflowInstance)
                    $Ctx.ExecuteQuery()
                    Write-host -f Green "Worfklow Terminated on List Item:" $ListItem.Id
                } Catch {
                    Write-host "Error terminating workflow on " $ListItem.Id " Error Details: $_"
                }
            }
        }
    }
    Catch {
    	Write-host -f Red "Error:" $_.Exception.Message
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Monday, January 27, 2020 3:30 AM
    Moderator
  • I ran the PS but it did not cancel workflow.

    not sure, what could be wrong? I check the list item count and it show 3 records, however when check $WorkflowAssociation.Name, it show nothing.

    Please help

    Thanks

    Monday, January 27, 2020 7:26 PM
  • Please help.

    Thanks

    Tuesday, January 28, 2020 3:28 PM
  • Hi,

    Did you use the SharePoint 2010 workflow?

    Try to create a new library and create a SharePoint 2013 workflow for the library, then run the PowerShell to check if it works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Mr. T1 Wednesday, January 29, 2020 7:09 PM
    Wednesday, January 29, 2020 9:51 AM
    Moderator