2

I have a custom edit page with some Javascript inserted using a CEWP.

From this page I am calling a function to start a list item workflow on the current item being edited.

I am using the following common function held in a separate library:

function startWorkflow(itemId, subId) {
        var context = SP.ClientContext.get_current(),
            web = context.get_web(),
            wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web),
            subService = wfServiceManager.getWorkflowSubscriptionService().getSubscription(subId);

        context.load(subService);

        context.executeQueryAsync(function () {
            console.log("Subscription was loaded, attempting to start workflow.");
            if (itemId != null && itemId != '' && itemId != undefined) {
                var itemParam = {};
                wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subService, itemId, itemParam);
            } else {
                console.log("Item Id was not provided.  Ending function.");
                return;
            }

            context.executeQueryAsync(function () {
                console.log("Successfully started workflow.");
            }, function (sender, args) {
                console.log("Failed to start the workflow.");
                console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
            });
        }, function (sender, args) {
            console.log("Failed to load the subscription.");
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
        });
    };

The function is running as far as the first executeQueryAsync, but then the 'fail' callback is being invoked, returning the "Failed to load the subscription." console.log with the args.message() being "Unexpected response from server".

As part of my debugging I am calling the same function from a custom site page and this function runs as expected with the workflow successfully starting on the required list item.

So, my question(s) are:

  1. Is it possible to start a list item workflow on an item currently being 'edited'?
  2. If so, where is my code wrong?
  • How would a workflow respond to a field being edited? If you type one letter it has another value. That would trigger a workflow on every keypress. So it sounds logical a Workflow can not run on items in edit mode. (I never tried it) – Danny '365CSI' Engelman Dec 30 '15 at 8:50
  • Well it seems I have two SE accounts on the go - I am the OP albeit with a different screen name. @DannyEngelman - Thanks, I take your point, but in my case the workflows (created in SPD2013) are set to manual start only, so would be invoke onEdit. In any case, the WF would only envoke when the item being edited was saved. – Fairfield Dec 31 '15 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.