0

I am trying to start a workflow on the host web from within a web part in an app. Here is my code:

var clientContext = new SP.ClientContext.get_current();
var hostWebUrl = hostUrl;
var hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, hostWebContext.get_web());

var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(subId);

clientContext.load(wfServiceManager);
clientContext.load(subscription);
clientContext.executeQueryAsync(function() {
    console.log("Subscription " + subscription.get_name() + " was loaded, attempting to start workflow.");
    var inputParameters = { };

    wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, inputParameters);

    clientContext.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());
});

I get an error at the wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, inputParameters); line, with the following shown in the console:

Subscription My Test Workflow for the App was loaded, attempting to start workflow.
Failed to start the workflow.
Error: Value cannot be null.
Parameter name: input
undefined

Does anyone have any idea what is wrong? My workflow is a simple workflow, it just updates a field; no input form.

0

The following code worked:

var clientContext = new SP.ClientContext.get_current();
var hostWebUrl = hostUrl;
var hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, hostWebContext.get_web());
var wfDeployService = wfServiceManager.getWorkflowDeploymentService();
var subService = wfServiceManager.getWorkflowSubscriptionService();

clientContext.load(subService);

clientContext.executeQueryAsync(function() {
    console.log("Subscription Service was loaded.  Attempting to get workflow subscription.");
    var subscription = subService.getSubscription(subId);
    console.log("Subscription was loaded, attempting to start workflow.");
    if (itemId != null && itemId != '' && itemId != undefined) {
        wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, new Object());
    } else {
        console.log("Item Id was not provided.  Ending function.");
        return;
    }

    clientContext.executeQueryAsync(function() {
        console.log("Successfully started workflow.");
        $("#lblResults").text("The workflow was successfully started.");
        $("#lblResults").css("color", "green");
        window.parent.postMessage('Closed', '*');
    }, 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());
});

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.