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.