locked
Stop all the running workflows on a list using SPServices RRS feed

  • Question

  • Hi All,

    We are working in SharePoint Online sites which have designer workflows associated with its lists.  Based on requirement, we have to make changes in workflow. After the changes, we've stop all the running workflows and restart it. Since, its SharePoint Online powershell script won't work.

    I am designing a tool which will input site URL and list name to stop and restart the list workflows. Using JSOM it couldn't terminate workflows for all items except the first or the last one. 

    Can we terminate and restart the worflows using jQuery SPServices? As I believe SPServices is very good in performing bulk operations

    Thanks,


    Kunal

    Friday, July 15, 2016 10:43 AM

Answers

  • Hi kunal,

    see below, hope will work this.

    https://gallery.technet.microsoft.com/Cancel-all-the-workflows-338d9caa

    # 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://[TENANT].sharepoint.com/"
    $ListName = "[TITLE OF THE LIST]"
    $UserName = "[USERNAME]"
    $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
       }
    }


    Mark as answer if help you.

    Thanks,


    Friday, July 15, 2016 11:04 AM
  • Hi Kunal,

    Please see below Url for your question, Hope it will help

    http://paulryan.com.au/2014/cancel-all-workflows/

    Thanks

    • Marked as answer by Kunal Basu Tuesday, January 24, 2017 9:27 AM
    Monday, July 18, 2016 1:02 PM
  • Kunal-

    The PowerShell script provided by Prashant is PowerShell CSOM script which is compatible with SharePoint online, only thing to make sure that you install SharePoint Client SDK in the workstation before running the script. For managing workflows, this script would be the most effective way then the SPServices JS library which relies on current user context.


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Monday, July 18, 2016 1:13 PM

All replies

  • Hi kunal,

    see below, hope will work this.

    https://gallery.technet.microsoft.com/Cancel-all-the-workflows-338d9caa

    # 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://[TENANT].sharepoint.com/"
    $ListName = "[TITLE OF THE LIST]"
    $UserName = "[USERNAME]"
    $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
       }
    }


    Mark as answer if help you.

    Thanks,


    Friday, July 15, 2016 11:04 AM
  • Hi Prashant,

    We are working in SharePoint Online environment where Powershell scripts won't help. I need solution in jQuery SPServices as its very effective for bulk operations.

    Thanks,


    Kunal

    Monday, July 18, 2016 10:28 AM
  • Hi Kunal,

    Please see below Url for your question, Hope it will help

    http://paulryan.com.au/2014/cancel-all-workflows/

    Thanks

    • Marked as answer by Kunal Basu Tuesday, January 24, 2017 9:27 AM
    Monday, July 18, 2016 1:02 PM
  • Kunal-

    The PowerShell script provided by Prashant is PowerShell CSOM script which is compatible with SharePoint online, only thing to make sure that you install SharePoint Client SDK in the workstation before running the script. For managing workflows, this script would be the most effective way then the SPServices JS library which relies on current user context.


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Monday, July 18, 2016 1:13 PM