Monday, January 12, 2015

Cancel not completed workflows using PowerShell in SharePoint 2013




We can cancel all workflows having status not equal to "Completed". Suppose you have a list which contains more than 1000 items and you realize the workflow which is attached to the list going to Suspended state or in progress state. Here if you want to cancel the workflows which are not completed, then it can not be a manual task when your list is larger. If you have 10/20 items then you can do manually, but in other case manual process is not a good option.

Also check out: Find larger files inside web application in SharePoint using PowerShell

I got a very good PowerShell approach from Raymun Macaalay's Dev Blog to cancel workflows. Thanks to the author for sharing this.

Below is the PowerShell script:
#Your Shaeproint Site URL
$web = Get-SPWeb "http://yoursharepointserver.com/yoursubsite";
$web.AllowUnsafeUpdates = $true;  
#Your List Name
$list = $web.Lists["YourListName"];
$count = 0
#Loop through all Items in List then loop through all Workflows on each List Items.        
foreach ($listItem in $list.Items)
{
 foreach ($workflow in $listItem.Workflows)
 {
  #Disregard Completed Workflows
  if(($listItem.Workflows | where {$_.InternalState -ne "Completed"}) -ne $null)
  {
   #Cancel Workflows      
   [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);    
   write-output "Workflow cancelled for : " $listItem.Title;
  }
 }
}
$web.Dispose();


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Favorites More