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