Run workflow using API

SYSTEMZ Flow for Office 365 > How To? > Run workflow using API

How To?


Run workflow using API


Useful pages:

  • Basic operations using SharePoint REST endpoints
  • REST API StartWorkflowOnListItem method
  • JavaScript API start workflow request:

    
    function startWorkflow(itemID, wfName)
    {
        function __startWorkflow()
        {
            var ctx = new SP.ClientContext.get_current(),
                wfsManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()),
                wfSubscriptions = wfsManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(_spPageContextInfo.pageListId);
            ctx.load(wfSubscriptions);
            ctx.executeQueryAsync(function() {
                var wfsEnum = wfSubscriptions.getEnumerator();
                while (wfsEnum.moveNext()) {
                    var wfSubscription = wfsEnum.get_current();
                    if (wfSubscription.get_name() == = wfName) {
                        console.log(wfSubscription.get_id());
                        wfsManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSubscription, itemID, new Object());
                        var note = SP.UI.Notify.addNotification('Init Workflow: ' + wfName + ' on item: ' + itemID, false);
                    }
                }
            });
        }
        if (SP.WorkflowServices) {
            __startWorkflow();
        }
        else {
            var script = document.createElement("script");
            script.src = "/_layouts/15/sp.workflowservices.js";
            script.onload = __startWorkflow;
            document.head.appendChild(script);
        }
    }
    startWorkflow(2, 'New ZFlow');

    REST API start workflow request:

    
    jQuery.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId='6af256bf-a5dc-4f96-a91b- 
        efd4b8c5ca90 ',itemId='
        2 ')",
        type: "POST",
        contentType: "application/json;odata=verbose",
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
        },
        success: function(data) {
            console.log('Workflow Trigger Successfully');
            console.log(data);
        },
        error: function(data) {
            console.log("Error");
            console.log(data);
        }
    });

    Request format:

    <"domain">/_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId='’,itemId='')

    Contents