1

I'm trying to get a count of all instances of a workflow by status. Below is the code I'm using. I have not been able to find a lot of information about WorkflowInstanceService.CountInstancesWithStatus from googling. The only thing I was able to find was this blop post.

http://www.bluepowersolutions.com/2017/04/14/How-to-get-the-number-of-suspended-workflow-instances-in-a-SharePoint-list.html

All I'm setting with this is an empty response. Any help with this is appreciated. Thanks.

function GetWorkflowStatus(subscriptionId, wfCountCellID){
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var context = SP.ClientContext.get_current();
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
var workflowSubscriptionService = workflowServicesManager.getWorkflowSubscriptionService();
var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
var subscription = workflowSubscriptionService.getSubscription(subscriptionId);
context.load(subscription); 
var stats = [1,4,5,6];
execute(context).then(function() {
    stats.forEach(function (st){
        var wfResp = workflowInstanceService.countInstancesWithStatus(subscription, st);
        var count = wfResp.get_value();
        execute(context).done(function() {
            document.getElementById(wfCountCellID + st).textContent = count;
        }).fail(function(message) {
            console.error(message);
        });
    });          
});

};

1

1 Answer 1

0

Worked it out based on below:

https://social.msdn.microsoft.com/Forums/office/en-US/eacfe6ff-0fd5-436a-866b-5ce64b4495c6/countinstanceswithstatus-returning-empty-response?forum=sharepointdevelopment

<script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
function GetWorkflowStatus(subscriptionId,workflowStatus){
    $.getScript("/_layouts/15/sp.js", function () {
        $.getScript("/_layouts/15/sp.workflowservices.js", function () {                 
            var context = SP.ClientContext.get_current();
            var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
            var workflowSubscriptionService = workflowServicesManager.getWorkflowSubscriptionService();
            var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
            var subscription = workflowSubscriptionService.getSubscription(subscriptionId);
            context.load(subscription);
            context.executeQueryAsync(
                function() {
                    $.each(workflowStatus,function(statusName, val) {
                        getCount(context,workflowInstanceService,subscription, val).done(function(result){
                            console.log(statusName+":"+result);
                        });     
                    }); 
                },
                function() {
                    console.log(arguments[1].get_message());
                }
            );          
        });
    });
}
function getCount(context,workflowInstanceService,subscription, status){
    var defer = jQuery.Deferred();
    var wfResp = workflowInstanceService.countInstancesWithStatus(subscription, status);
    context.executeQueryAsync(
        function() {
            var count = wfResp.get_value();
            defer.resolve(count);
        },
        function() {
            defer.reject(arguments[1].get_message());
        }
    );
    return defer.promise();
}
$(function () {
    var subscriptionId="B7EACCFC-8760-4660-BC04-CC7C0A400FA3";   
    var workflowStatus ={
                           NotStarted : 0,
                           Started : 1,
                           Suspended : 2,
                           Canceling : 3,
                           Canceled : 4,
                           Terminated : 5,
                           Completed : 6,
                           NotSpecified : 7,
                           Invalid : 8
                       };
    GetWorkflowStatus(subscriptionId,workflowStatus);
});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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