Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I need to be able to get the status of a SharePoint workflow 2013 using javascript.

My goal is to start in a loop for all selected items synchronously a workflow. For that I will check if the started Workflow is completed before I start the next one.

/* Run workflows for multiple list items */
function StartWorkflowForSelectedItems() {
    showInProgressDialog();
    var ctx = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var subID = '{BE3F6F70-E1E3-4105-8351-0DFB246EA451}';
    var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
    var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subID);
    ctx.load(subscription, 'PropertyDefinitions');
    ctx.executeQueryAsync(
        function (sender, args) {
            var params = {};
            for (var i = 0; i < selectedItems.length; i++) {
                var itemId = selectedItems[i].id;
                var test = wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);

               // wait until Workflow is Complete
            }
        ctx.executeQueryAsync(
            function (sender, args) {
                closeInProgressDialog();
                reloadWindow()

            },
            function (sender, args) {
            closeInProgressDialog();
            alert('Failed to run workflow');
            }
        );
},
function (sender, args) {
    closeInProgressDialog();
    alert('Failed to run workflow');
}
   );
}

Does anyone have any ideas or code around this?

//Edit

Thanks for your answer, but when I include your solution in my code shown below I get an exception:

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

for var workflowStatusValue = listItem.get_item(workflowTitle); //get workflow status field value

What I´m doing wrong?

/* Run workflows for multiple list items */
function StartWorkflowForSelectedItems() {

    showInProgressDialog();
    var ctx = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var subID = '{5A407502-2F30-41AE-A43E-0FA62CC4F7C3}';
    var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
    var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subID);

    for (var i = 0; i < selectedItems.length; i++) {
        var itemId = selectedItems[i].id;


    ctx.load(subscription, 'PropertyDefinitions');
    ctx.executeQueryAsync(
        function (sender, args) {
            var params = {};

            wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);

                // wait until Workflow is Complete

            ctx.executeQueryAsync(
                function (sender, args) {
                    closeInProgressDialog();
                    reloadWindow()

                },
                function (sender, args) {
                    closeInProgressDialog();
                    alert('Failed to run workflow');
                }
            );
        },
function (sender, args) {
    closeInProgressDialog();
    alert('Failed to run workflow');
}
   );



    //Example: check whether workflow has been completed for a List Item
    var listTitle = 'Agendapunkte';
    var listItemId = itemId;
    var workflowTitle = 'verschieben von Agendapunkte nach AgendaProtokoll und Meeting';
    getWorkflowStatus(listTitle, listItemId, workflowTitle, function (status) {
        if (status == 5) {
            console.log('Approval is completed');
        }
    },
        function (sender, args) {
             console.log(args.get_message());
        });
    }
}


function getWorkflowStatus(listTitle, listItemId, workflowTitle, success, error) {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    var listItem = list.getItemById(listItemId);
    context.load(listItem);
    context.executeQueryAsync(
       function () {
           var workflowStatusField = listItem.get_parentList().get_fields().getByTitle(workflowTitle); //get workflow status field
           var workflowStatusValue = listItem.get_item(workflowTitle); //get workflow status field value
           success(workflowStatusValue);
       },
       function () { alert("error")}
    );
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

How to get Workflow status via CSOM

function getWorkflowStatus(listTitle,listItemId,workflowTitle, success,error)
{
  var context = new SP.ClientContext.get_current();
  var web = context.get_web();
  var list = web.get_lists().getByTitle(listTitle);
  var listItem = list.getItemById(listItemId);   
  context.load(listItem);
  context.executeQueryAsync(
     function() {
       var workflowStatusField = listItem.get_parentList().get_fields().getByTitle(workflowTitle); //get workflow status field
       var workflowStatusValue = listItem.get_item(workflowTitle); //get workflow status field value
       success(workflowStatusValue); 
     },
     error
  );
}



//Example: check whether workflow has been completed for a List Item
var listTitle = 'Pages';
var listItemId = 1;
var workflowTitle = 'Approval';
getWorkflowStatus(listTitle,listItemId,workflowTitle,function(status){
   if(status == 5){  
       console.log('Approval is completed');
   } 
},
function(sender,args){
   console.log(args.get_message());
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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