0

I am trying to get the list of all workflows in Suspended state

I looked up in google and the max that I can get is the list of workflows that are enabled in one list

_api/SP.WorkflowServices.WorkflowSubscriptionService.Current/EnumerateSubscriptionsByList('guid in here')

I also tried modifying the parameters in the workflow.aspx URL. I tried removing the ID parameter etc, but nothing worked

_layouts/15/workflow.aspx?List={DJKD33-L7S2-93J3-90B7-L5G0FA73E562}&Source=/workflows/Lists/List Name&ID=596&ShowAll=1

I am able to see the status of all workflows in the site from this url

_layouts/15/workflowservicehealth.aspx

But there is not option to see the list of suspended or terminated workflows.

Is there a way to list of all suspended workflows (list workflows) using REST api/ Search API or Javascript(JSOM) ? or are there any urls?

1

May not be pretty, but this is what I put together to alert me when workflows are suspended. Replace these with your values: {SP_SITE_URL},{LIST_NAMEX},{EMAIL},{smtpSERVER} Now I'm trying to find out how to terminate these from REST.

clear-host


    $spSiteUrl = "{SP_SITE_URL}"

    $lists = @('{LIST_NAME1}','{LIST_NAME2}','{LIST_NAME1}')




    ############################################################################################################################################
    #Get Form Digest
    ############################################################################################################################################
    function GetRestFormDigest {
        if (((Get-Date) -gt $spDigestExpires) -or (!$spFormDigest)) {
            $hHeaders = @{
                "accept" = "application/json;odata=verbose"
            }

            $spRESTUrl = $spSiteUrl + "/_api/contextinfo"
            $spRESTResults = Invoke-RestMethod -Uri $spRESTUrl -UseDefaultCredentials -Method POST -Headers $hHeaders

            $global:spDigestTimeout = $spRESTResults.d.GetContextWebInformation.FormDigestTimeoutSeconds
            $global:spFormDigest = $spRESTResults.d.GetContextWebInformation.FormDigestValue

            $global:spDigestExpires = (Get-Date).AddSeconds($spDigestTimeout - 1)
        }
    }

    ############################################################################################################################################
    #Get List ID
    ############################################################################################################################################
    function GetListID ($spListName){
        $ErrorActionPreference = "Stop"

        $spRESTUrl = $spSiteUrl + "/_api/web/lists/getbytitle('" + $spListName + "')/"

        $spRESTResults = Invoke-RestMethod -Uri $spRESTUrl -UseDefaultCredentials -Method GET -Headers $hHeaders

        $spListItemEntityTypeFullName = $spRESTResults.d.ListItemEntityTypeFullName
        $spListID = $spRESTResults.d.id

        $spRESTUrl += "items"

        $spListItemRTN = @{}
        $spResults = @{}
        $rResult = @{}

        $spRESTResults = Invoke-RestMethod -Uri $spRESTUrl -UseDefaultCredentials -Method GET -Headers $hHeaders

        $spRESTResultsCorrected = $spRESTResults -creplace '"Id":', '"fake_id":'

        $spJSONResults = $spRESTResultsCorrected  | ConvertFrom-Json
        $spListItems = $spJSONResults.d.results

        while ($spJSONResults.d.__next) {
            $spRESTResults = Invoke-RestMethod -Uri $spJSONResults.d.__next -UseDefaultCredentials -Method GET -Headers $hHeaders
            $spRESTResultsCorrected = $spRESTResults -creplace '"Id":', '"fake_id":'
            $spJSONResults = $spRESTResultsCorrected  | ConvertFrom-Json
            $spListItems += $spJSONResults.d.results
        }

        GetRestFormDigest
        $hHeaders = @{
            "accept" = "application/json;odata=verbose";
            "content-type" = "application/json;odata=verbose";
            "content-Length" = $bBody.Length;
            "X-RequestDigest" = $spFormDigest;
            "X-FORMS_BASED_AUTH_ACCEPTED" = "f";
            "Host" = "{SP_HOST}
        }
        write-host $spListName  -BackgroundColor darkyellow
        write-host $spListItems.Count "List Items"
        write-host "---"
        write-host "ItemId - RTN - Status - Created - Updated"
        $suspended = 0
        $c = 0 
        for ($i = 0; $i -lt $spListItems.Count; $i++) {
            $c++; write-progress 'List Items' -PercentComplete ($c / $spListItems.Count * 100)

            $spWorkflows = [ordered]@{}
            $spListItemID =  $spListItems[$i].id

            $spRESTUrl = $spSiteUrl + "/_api/SP.WorkflowServices.WorkflowInstanceService.Current/EnumerateInstancesForListItem(listId='"+$spListID+"',itemId='"+$spListItemID+"')"
            $spRESTResults = Invoke-RestMethod -Uri $spRESTUrl -UseDefaultCredentials -Method POST -Headers $hHeaders -Body $bBody -OutVariable resp

            $spJSONResults = $spRESTResultsCorrected  | ConvertFrom-Json
            $spListItems = $spJSONResults.d.results

            $spWorkflowInstances = $spRESTResults.d.results
            $oldestDate = (get-date 1900-01-01)
            $oldestKey = $null

            for ($w = 0; $w -lt $spWorkflowInstances.Count; $w++) {
                $spListItemID =  $spListItems[$w].id

                if($spWorkflowInstances[$w].Status -eq 2){
                    $suspended++
                    switch ( $spWorkflowInstances[$w].Status )
                    {
                        2 { $result = 'Suspended'; $color='red' }
                        5 { $result = 'Terminated'; $color=$null }
                        6 { $result = 'Completed'; $color=$null  }
                        default { $result = $spWorkflowInstances[$w].Status; $color=$null }
                    }
                if($color){
                write-host $spListItems[$i].id "-" $spListItems[$i].RTN "-" $spWorkflowInstances[$w].Status "=" $result "-" $spWorkflowInstances[$w].InstanceCreated " - " $spWorkflowInstances[$w].LastUpdated -BackgroundColor $color
                }else {
                write-host $spListItems[$i].id "-" $spListItems[$i].RTN "-" $spWorkflowInstances[$w].Status "=" $result "-" $spWorkflowInstances[$w].InstanceCreated " - " $spWorkflowInstances[$w].LastUpdated
                }
                    }
                if((get-date $oldestDate) -lt (get-date $spWorkflowInstances[$w].LastUpdated)){
                    $oldestDate = $spWorkflowInstances[$w].LastUpdated
                    $oldestKey = $w
                }
            }
        }
        write-progress 'List Items' -Completed
        write-host $suspended "Suspended Workflows" -BackgroundColor darkyellow
        write-host ""
        write-host ""
    }

#
#===========================================================================================================================================
#Main
#===========================================================================================================================================
#
$hHeaders = @{
    "accept" = "application/json;odata=verbose"
}

$logfile = "{PATH}"+$MyInvocation.MyCommand.Name+"_"+(Get-Date).tostring("yyyy-MM-dd")+"_log.txt"

Start-Transcript -path $logfile

foreach($list in $lists){GetListID $list }

Stop-Transcript

$regexScript = '(\d+) Suspended Workflows'

foreach($line in Get-Content -Path $logfile){
    if($line -match $regexScript){
        $val = [regex]::Match($line,$regexScript).captures.groups[1].value
        #write-host $val
        if($val -ne 0){
            $Date = Get-Date
            $Body = @()
            $Body += $Date
            $Body += "<BR>Check Script Logs:<BR>"
            $Body += "Ran by: "+([Environment]::UserDomainName + "\" + [Environment]::UserName)+"<br>"

            foreach($key in $err.keys){
                if($err[$key] -match 'Script\: '){
                    $Body += "<p><b>"+[STRING]$err[$key]+"</b><BR>"
                }
                else{
                $Body += [STRING]$err[$key]+"<br>"
                }
            }

            $Body = [STRING]$Body

            Send-MailMessage -BodyAsHtml -From {EMAIL} -To {EMAIL} -Subject "Suspended Workflows" -SmtpServer {smtpSERVER} -Body $Body -Attachments $logfile


            break
        }
    }

}
  • Congrats on your first answer, upvoted it. Good luck with SO – Vignesh Subramanian Dec 21 '18 at 3:42

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.