3

I have a SP2013 list item running a SP2010 Platform Workflow. The workflow will pause until a specified date then perform some actions. This workflow should be started when an item is created and changed. However, when the item is changed the specified date to resume actions is changed, but as is nothing happens because a workflow instance is already running.

I would like to add some javascript to the the EditForm PreSave function, which would cancel any "In Progress" workflows for the given item. This would allow for a new workflow instance to be started with the changed date.

I am loading the sp.runtime.js and sp.workflowservices.js in the page header.

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.workflowservices.js"></script>
</asp:Content>

I am ensuring that the sp.js script file is loaded before calling the cancel/terminate function.

SP.SOD.executeFunc("sp.js", "SP.ClientContext", function (){
    CancelWorkflow(wfInstanceID);
});

The Problem

Internet Explorer 11 is the target browser. Although I have tested in Firefox with the same results.

When I add a context.load and context.executeQueryAsync after the call to SP.WorkflowServices.WorkflowServicesManager it fails.

function CancelWorkflow(subId){
    var context = SP.ClientContext.get_current();
    var wfServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
    context.load(wfServicesManager);
    context.executeQueryAsync(
        function (sender, args) {
            var wfInteropService = wfServicesManager.getWorkflowInteropService();
            context.load(wfInteropService);
            context.executeQueryAsync(
                function (sender, args) {
                    wfInteropService.cancelWorkflow(subId);
                    context.executeQueryAsync(
                        function (sender, args) {
                            console.log("Cancelation Successful");
                        },
                        function (sender, args) {
                            console.log("Failed to cancel workflow.");
                            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
                        }
                    );
                },
                function (sender, args) {
                    console.log("Failed to load WorkflowInteropService.");
                    console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
                }
            );
        },
        function (sender, args) {
            console.log("Failed to load WorkflowServicesManager.");
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
        }
    );
}

Failed to load WorkflowServicesManager.

Error: Value cannot be null.

Parameter name: uriString

undefined


When I only have the context.load and context.executeQueryAsync after the call to wfInteropService.cancelWorkflow it fails.

function CancelWorkflow(subId){
    var context = SP.ClientContext.get_current();
    var wfServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
    var wfInteropService = wfServicesManager.getWorkflowInteropService();
    wfInteropService.cancelWorkflow(subId);
    context.executeQueryAsync(
        function (sender, args) {
            console.log("Cancelation Successful");
        },
        function (sender, args) {
            console.log("Failed to cancel workflow.");
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
        }
    );
}

Failed to cancel workflow.

Error: Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "GetWorkflowInteropService new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager()"

undefined


It seems as though the WorkflowServicesManager is not being returned correctly.

Any help with this would be greatly appreciated.


With the help of the following post, I think that I may have found the answer. Our server admins tell me that the Workflow Manager is not installed.

Unable to cancel a 2010 workflow in SharePoint 2013 using JSOM

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.