3

I'm trying to script up a simple button on a display form that triggers a workflow on the item being viewed. I've been through a dozen different posts/tutorials on this and I can almost get it working, but they pretty much all require knowing the workflow's subscription ID in order to work. In earlier versions of SP it was pretty easy to do; You just need to inspect the link that starts the workflow. But in my case, the link simply displays an error message about needing to edit the workflow in SPD.

I've tried looking at it in SPD but it doesn't seem to list anything about the subscriptions. I've also tried getting to it through PowerShell, but the closest I can get is finding the workflow properties from the service proxy, which also doesn't give the subscription ID. I'm at a loss for where to get it. What am I missing here?

For reference, here's a sample of code that requires it:

function StartWorkflow(subscriptionId, itemId) {
   showInProgressDialog();
   var ctx = SP.ClientContext.get_current();
   var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
   var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);
   ctx.load(subscription, 'PropertyDefinitions');
   ...

Edit: Although there is this script which mentions the subscription ID is not the preferred way, but the code in there is a little beyond my comfort zone so I'm not entirely sure how to modify it.

Furthermore, I need to clarify that the workflow I'm targeting is on the 2010 platform, not 2013.

1 Answer 1

1

I posted this on SO before:

https://sharepoint.stackexchange.com/search?q=user%3A32871+start+workflow

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) {
                    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(12,'MyWorkflowName');
3
  • This would work on the 2013 workflow platform, however I've discovered that it does not work for 2010 workflows. It returns null when trying to enumerate the WF I'm targeting.
    – thanby
    Commented Dec 20, 2017 at 14:08
  • Had your question been tagged 2010 instead of 2016, I would have added that it works in 2013+ Commented Dec 20, 2017 at 19:02
  • Not your fault, I wasn't aware when I asked that the distinction need be made. It was still helpful though, because I learned a lot about the internals of the workflow management system while exploring it.
    – thanby
    Commented Dec 20, 2017 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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