0

I am in the process of developing a workflow for my company, and I have noticed that my workflows are not terminating properly. If you go to the workflow settings for a list, it will show a very high amount of active workflows yet there isn't that many.

My current workflow shows 165 workflows in progress, but when I run this script, it only shows 16 active workflows:

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$web = get-spweb -Identity ************
$list = $web.Lists["**List in Question**"]

$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
$wf = $sub.EnumerateSubscriptionsByList($list.ID)
$wfis=$wfm.GetWorkflowInstanceService()

$test

foreach ($item in $list.Items)
{        
    $workflowInstances=$wfis.EnumerateInstancesForListItem($list.ID,$item.ID)

    foreach($wf in $workflowInstances) 
    {
        $wfName = $wf | ?{$_.Id -eq $wf.WorkflowSubscriptionId} | select -ExpandProperty Name
        $wfID= $wf.Properties.'Microsoft.SharePoint.ActivationProperties.CurrentItemUrl'
        $wfStatus = $wf.Status
        $wfListItem = $item.Name
        $wfListItemID = $item.ID

        write-host "Workflow URL: " -ForegroundColor Green -NoNewline
        write-host $wfID -NoNewline
        write-host " Status: " -ForegroundColor Green -NoNewline
        write-host $wfStatus -NoNewline
        write-host " ListItem: " -ForegroundColor Green -NoNewline
        write-host $wfListItem -NoNewline
        write-host " ListItem ID " -ForegroundColor Green -NoNewline
        write-host $wfListITemID
    }    
}

Most of these workflows were from items that were deleted. Can anyone advise on why deleting an item isn't properly terminating the workflows?

0

Check if this helps you

https://www.codeproject.com/Articles/679628/Easiest-Way-of-Cancelling-All-S

Basically, you need to check the status and cancel it:

 if($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')
              {
                [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow); 
              }

Hope this gives you some idea!

2
  • I just tried this and it didn't seem to work. This is ran based off of the the the workflows on list items, but according to my PowerShell there are no items in the list.
    – Rbenjamin
    May 19 '17 at 12:24
  • The code posted by @user2598808 is intended for WF2010. You are using WorkflowServicesManager which manages 2013WFs.
    – Emaborsa
    May 16 '18 at 14:44

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.