Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Easiest Way of Cancelling All SharePoint Workflows in Progress

4.50/5 (2 votes)
7 Nov 2013CPOL 34.9K  
The easiest way to cancel all SharePoint workflows in progress

Do you have a really big list in SharePoint and have a workflow attached to it? Had you encountered an issue where this workflow suddenly stopped and left a good number of Workflows in progress?

Workflow Settings

Have you ever wondered where you find these workflows in progress? Going to each list one by one will be OK if you have 10 items on your SharePoint List but if you have 1000, that can be a daunting task cancelling them all by going to each list item. Well, things can be easier by using SharePoint Powershell where you can iterate to all list items that have workflows attached to them and cancel them programmatically if they confirm to a certain condition, like when they are not “Completed” or “Suspended”.

I had the same issue last week and I had to stop all of them and this is how I did it in Powershell.

#Your SharePoint 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();

You can also change the condition based on what your requirements are. For a full list of what’s available, please check it here.

Filed under: CodeProject, Programming, Tips
Tagged: Powershell, Sharepoint 2013, Sharepoint Foundation 2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)