SharePoint 2010: Cancel all Workflows in a library using PowerSHell

 This is a script that I used when I needed to cancel all running workflows in a document library. Copy/paste from this site so this info isn't lost

https://www.sharepointdiary.com/2014/11/cancel-workflows-in-sharepoint-with-powershell.html


Cancel Workflows in SharePoint using PowerShell

Requirement: There was a large list with workflow attached to it. SharePoint Development team came with a requirement of cancelling multiple workflows running on these list items, nearly 2000!

While cancelling workflows on individual item is pretty straight forward, How about cancelling workflows on 1000's of items? Would be a daunting task, isn't it?
terminate workflow in sharepoint using powershell

Terminate Workflow in SharePoint using PowerShell:
Well, PowerShell can help to cancel workflows in SharePoint. If you ever have to cancel multiple running workflows on all list items, use this PowerShell script:

Cancel all workflows on a list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$web = Get-SPWeb "http://your-sharepoint-site-url"
 
#List Name
$list = $web.Lists["Your-List-Name"]
 
# Iterate through all Items and all Workflows on Items
foreach ($item in $list.Items)
 {
   foreach ($wf in $item.Workflows)
     {
 #Cancel Workflows       
 [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)     
     }
  }
To cancel all errored workflows use the condition as:
1
2
3
4
5
6
7
8
9
10
11
foreach ($item in $list.Items)
 {
   foreach ($wf in $item.Workflows)
     {
        if($wf.InternalState -match 'Error')
         {
     #Cancel Workflows       
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);     
         }
     }
 }

Cancel Workflow in SharePoint 
Lets target a particular workflow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
$web = Get-SPWeb "http://your-sharepoint-site-url"
 
#Get the List
$list = $web.Lists["Your-List-Name"]
 
#Get the specific workflow, Associated with the list
$WorkFlowToCancel = "Approval Workflow"
 
# Iterate through all Items and all Workflows on Items
foreach ($item in $list.Items)
 {
   foreach ($wf in $item.Workflows)
     {
     #Check for the particular workflow
        if( ($wf.ParentAssociation.Name -eq $WorkFlowToCancel) -and ($wf.IsCompleted -ne $true) -and($wf.StatusText -ne "Canceled"  ))
        {
            write-host "Previous workflow status:"  $wf.InternalState
            #Cancel Workflow
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)     
            write-host "Workflow Cancelled at $($list.title)! "
        }
     }
  }

Related Post: How to Start a SharePoint Workflow using PowerShell



#Read more: https://www.sharepointdiary.com/2014/11/cancel-workflows-in-sharepoint-with-powershell.html#ixzz6VTga5hiM

Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.