0

I have a web part page where a list view is added and need to start workflows on items in this list using java script, on click of a button. Below is the code snippet, which fails with error message

"invalid Request"

.

Response
    [
    {
"SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.5228.1203","ErrorInfo":{
"ErrorMessage":"Invalid request.","ErrorValue":null,"TraceCorrelationId":"c8fa7c9d-10e8-3000-0a9b-5db5f926b721"
,"ErrorCode":-1,"ErrorTypeName":"Microsoft.SharePoint.Client.InvalidClientQueryException"
},"TraceCorrelationId":"c8fa7c9d-10e8-3000-0a9b-5db5f926b721"
}
]

Code:

function SendStatusEmail()
{
var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";    
$.getScript(scriptbase + "SP.js", function () {
    $.getScript(scriptbase + "SP.Runtime.js", function () {
        $.getScript(scriptbase + "SP.WorkflowServices.js", function () {
            StartWorkflow();
        })
    })
});
}

function StartWorkflow() {

var listGuid = _spPageContextInfo.pageListId;
JSRequest.EnsureSetup()
var itemId = JSRequest.QueryString['ID'];
var workflowName = 'WFsendemail';
var initiationParameters = null
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Contract_Status');
var item = list.getItemById(itemId);

clientContext.load(web);
clientContext.load(list);
clientContext.load(item);

var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, web);
clientContext.load(servicesManager);
var subs = servicesManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(listGuid);
clientContext.load(subs);

clientContext.executeQueryAsync(function () {
    var subsEnum = subs.getEnumerator();
    var workflowSub;

    while (subsEnum.moveNext()) {
        var sub = subsEnum.get_current();
        var subName = sub.get_name();

        if (subName == workflowName) {
            workflowSub = sub;
            break;
        }
    }

    if (workflowSub) {
        console.log('Web: ' + web.get_url() + ', Subscription: ' + workflowSub.get_name() + ', id: ' + workflowSub.get_id());

        var initiationParams = initiationParameters || {};
        servicesManager.getWorkflowInstanceService().startWorkflowOnListItem(workflowSub, itemId, initiationParams);

        clientContext.executeQueryAsync(function (sender, args) {
            console.log('Workflow started.');
        }, Function.createDelegate(this, logError));

        callback();
    }
    else {
        logError(this, {
            get_message: function () {
                return 'Can not find the workflow:' + workflowName;
            }
        });
    }
}, Function.createDelegate(this, logError));

function logError(sender, args) {
    // Log error to console
    console.log(args.get_message());

    // Handle Errors
};
};

function callback() {
console.log("callback executed");
}

Same code is used on Edit item form and its working. Need help on this. Thanks in advance.

1

The issue was with ClientContext object. It was not loading properly. Initially error was : "invalid Request".

On next day it started showing below error( :O )

"Object doesn't support property or method 'set_formDigestHandlingEnabled' "

After googling this error, I found that SP.Runtime.js and SP.js files were not loaded in sequence. I had to load SP.Runtime.js before SP.js

reference link:SP2013 online : set_formDigestHandlingEnabled Error

Your Answer

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

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