Click here to Skip to main content
15,881,882 members
Articles / Database Development / SQL Server
Technical Blog

Easiest Way of Cancelling All SharePoint Workflows in Progress

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
7 Nov 2013CPOL 34.4K   1   2
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)


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
QuestionBetter options - script Pin
322222221-Apr-14 0:15
322222221-Apr-14 0:15 
SuggestionA more efficient variation Pin
Paul Ryan6-Jan-14 0:20
Paul Ryan6-Jan-14 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.