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

Instead on canceling workflows, you will probably want to make your updates without triggering workflows. There are two options. One, make you change and then call .SystemUpdate($false) instead of .Update(), or disable event firing by changing the EventFiringDisabled disabled property.

See an example here:
https://www.sharepointsapiens.com/blog/update-item-document-properties-without-changing-modified-modified-fields-using-sharepoint-powershell/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.