Tuesday, April 18, 2017

Check the Terminated Workflow using JSOM

We have a functionality on a application where admin user can terminate the current running instance of a list item Workflow. It's was a difficult situation for us as workflow didn't update any value in the list or Task list for termination of current instance. And the task which was created by workflow remains in the Not started state if no activity made on task before the Workflow termination

After digging about this issue on internet we come to know that workflow gives a Fault Information about the current running instance on List item.

That Fault information contains a string value "Terminated"

Here is complete code to check the Workflow status for Terminated.

Please ignore showhidedive function in this code.

function CheckWFTerminated() {
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
        SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));
        SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager", getWorkflowStatus);
    })
}
function getWorkflowStatus() {
    GetItemIdByProjectName().done(function (ItemId) {
        getListGUID(ItemId).done(function (ListGUID) {
            WorkflowInstance(ListGUID, ItemId).done(function (wfInstanceCollection) {
                console.log(wfInstanceCollection);
                var testatus = $('#hfStatus').val();
                if (wfInstanceCollection.get_count() > 0) {
                    var wfInstance = wfInstanceCollection.getItemAtIndex(0);
                    var strTerminated = wfInstance.get_faultInfo();
                   
                    if (!IsNullOrUndefined(strTerminated)) {
                        if (strTerminated.indexOf("Terminated") > 0) {
                            getWorkflowTasks(ItemId).done(function (WorkflowTasks) {
                                var listItemEnumerator = WorkflowTasks.getEnumerator();
                                while (listItemEnumerator.moveNext())
                                {
                                    var oListItem = listItemEnumerator.get_current();
                                    var TaskId = oListItem.get_item("ID");
                                    setWorkflowTasksReject(TaskId).done(function (msg) { console.log(msg); });
                                }

                                $('#hfStatus').val(StatusVal_PI_Submitting);
                                showhidedive(StatusVal_PI_Submitting);

                            });
                        }
                        else { showhidedive(testatus); }
                    }
                    else { showhidedive(testatus); }
                }
                else { showhidedive(testatus); }
            });
        });
    });
}

Supporting function


function WorkflowInstance(ListGUID, itemId) {
    var dfdcpname = $.Deferred(function () {
        var ctx = SP.ClientContext.get_current();
        var wfServicesManager = new SP.WorkflowServices.WorkflowServicesManager(ctx, ctx.get_web());

        var wfInstanceService = wfServicesManager.getWorkflowInstanceService();
        var wfInstanceCollection = wfInstanceService.enumerateInstancesForListItem(ListGUID, itemId);
        //var wfInstanceCollection = wfInstanceService.enumerate(sub);
        ctx.load(wfInstanceCollection);
        ctx.executeQueryAsync(function (sender, args) {

            dfdcpname.resolve(wfInstanceCollection);
        }, function (sender, args) {
            dfdcpname.reject(args.get_message());
        });
    });
    return dfdcpname.promise();
}
function getListGUID() {
    var dfdcpname = $.Deferred(function () {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var PROLIST = $("#hfProList").val();
    var oList = web.get_lists().getByTitle(PROLIST);
    context.load(oList);
    context.executeQueryAsync(
         function () {
             dfdcpname.resolve(oList.get_id());
         },
         function (sender, args) {
             dfdcpname.reject(args.get_message());
         });
    });
    return dfdcpname.promise();
}
function GetItemIdByProjectName() {
    var projectname = $("#projectname").val();
    var dfdcpname = $.Deferred(function () {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var PROLIST = $("#hfProList").val();
        var list = web.get_lists().getByTitle(PROLIST);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + projectname + "</Value></Eq></Where></Query></View>");
        var collListPName = list.getItems(camlQuery);
        context.load(collListPName);

        context.executeQueryAsync(function () {
            var listItemEnumerator = collListPName.getEnumerator();
            var ItemId = 0;
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                ItemId = oListItem.get_id();
                break;
            }
            dfdcpname.resolve(ItemId);
        },
       function (sender, args) {
           dfdcpname.reject(args.get_message());
       });
    });
    return dfdcpname.promise();
}

No comments:

Post a Comment