9

Looking to launch a list workflow from script tied to a button on a form. I've been referencing this article, as well as the code from example 5, and even tried using SPServices, but have yet to come up with a working solution.

The example from codeproject fails when it tries to declare the servicesManager variable citing that the property "WorkflowServicesManager" is unavailable as "SP.WorkflowServices" is undefined.

Per Andrew Connell's article, I've included:

<SharePoint:ScriptLink runat="server" Name="SP.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="SP.Runtime.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="SP.WorkflowServices.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>

The body of the script is also wrapped in:

ExecuteOrDelayUntilScriptLoaded(function () {...}, "SP.js");

Can I get a pointer as to where I'm missing the required dependency to access the JavaScript Workflow Services API for SharePoint 2013?

Thanks very much.

EDIT: So I'm still struggling to initialize SP.WorkflowServices.WorkflowServicesManager, and have gone into the workflows menu for the list item in question to see how they are launched natively. SharePoint's link to launch the workflow looks like:

<a href="javascript:StartWorkflow4('SubscriptionID', 'ItemID', '{GUID}')">

The first two parameters were easy enough to identify, but the third GUID is unknown to me, and I can't seem to find any documentation regarding StartWorkflow4(). The Dev Center article on StartWorkflow methods doesn't appear to have anything that corresponds directly with what I'm seeing here either. If I can identify the third parameter, I can probably figure out how to retrieve it, and make a similar function call on click from another page.

EDIT2:

The SharePoint:ScriptLink tags have been replaced with script tags and the API is now accessible.

<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.workflowservices.js"></script>

Functional code, assuming no input parameters required:

function startWorkflow(itemID, subID) {

    var context = SP.ClientContext.get_current();
    var web = context.get_web();

    var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);

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

    context.load(subscription);

    context.executeQueryAsync(
        function(sender, args){
            console.log("Subscription load success. Attempting to start workflow.");        
            var inputParameters = {};

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

            context.executeQueryAsync(
                function(sender, args){ console.log("Successfully starting workflow."); },
                function(sender, args){ 
                    console.log("Failed to start workflow.");
                    console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
                }
            );
        },
        function(sender,args){
            console.log("Failed to load subscription.");
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
        }
    );
}
4
  • Go into devtools for IE (F12), and look at the 'Script' tab. Verify that all of your sharepoint js libraries you mentioned are being loaded when you visit that page.
    – tyshock
    Feb 4, 2014 at 20:22
  • Replacing the ScriptLink tags with standard <script> tags and referencing the relative paths opened this up. Now I'm just plodding through the syntax.
    – Joe
    Feb 6, 2014 at 21:50
  • Please check out the following article it describes very similar case plumsail.com/blog/2015/02/…
    – Roman
    Aug 24, 2016 at 15:25
  • I get access denied when I try this approach with Contribute and edit permissions
    – Mike
    Jul 5, 2019 at 15:29

2 Answers 2

8

Thanks for this!

This comes in handy when you want to click on a link within a column to start the WF. To do so, add a calculated column and select the number type.

and use the following code:

=TEKST.SAMENVOEGEN("<a href=javascript:StartWorkflow(";ApparaatID;",";"'";"{e5859b62-c666-41d0-94be-4df08b86e8d4}";"'";")>ingeleverd</a>")

Sorry for the Dutch text but TEKST.SAMENVOEGEN should be CONCATENATE and beware of the comma.

ApparaatID is the current itemID that I made available via a small SPD WF (only starts on create new item).

This will create a link in your list and fires the workflow. Copy Shortcut gives me: javascript:StartWorkflow(7,'{e5859b62-c666-41d0-94be-4df08b86e8d4}')

Added a reload delay function + the WF is starting dialog. So that you receive some feedback and you will see the result in your list.

What I've added:

//dialog element to show during processing
var dlg = null; 

//reload delay
function aReload() {
  location.reload(true);
  }

function startReload() {
  setTimeout("aReload()", 2000);
  }

and

//close dialog
function closeInProgressDialog() {
   if (dlg != null) {
       dlg.close();
   }
}
//open dialog
function showInProgressDialog() {
   if (dlg == null) {
       dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait...", "Waiting for workflow...", null, null);
   }
}

to start these add:

closeInProgressDialog();
startReload();

I hope this will save some time for you guys.

Cheers, Amancio

1
  • I assume the guid is for the workflow and not the list?
    – Rothrock
    Mar 2, 2017 at 16:22
1

Just to answer the third parameter of the below JavaScript function. javascript:StartWorkflow4('SubscriptionID', 'ItemID', '{GUID}')

Both ItemID and {GUID} are unique identifiers of the item. One is integer type and another one is GUID type.

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.