Quantcast
Channel: SharePoint 2013 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 26374

Cancel a workflow when a item is changed in a list using PowerShell

$
0
0
I found code to cancel all workflows that are completed using powershell. However I was looking for code (using powershell) that will cancel a workflow (In Progress) on a specific workflow for a specific list when a current item: called "pending" is changed to a different value. What code would work using powershell?

Here is the code I found: 

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

Viewing all articles
Browse latest Browse all 26374

Trending Articles