I am using a SharePoint 2013 state machine workflow. In my page I attempt to start the workflow using below both ways
Method 1 - using Item ID & WF subscription name
var wfName = "notification workflow";
var context = SP.ClientContext.get_current();
var web = context.get_web();
var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var wfSubs = wfServiceManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList('89fce79e-ced2-44c1-8ba0-9ea015a2d059');
context.load(wfSubs);
context.executeQueryAsync(function () {
wfsEnum = wfSubs.getEnumerator();
while (wfsEnum.moveNext()) {
var wfSub = wfsEnum.get_current();
if (wfSub.get_name() === wfName) {
wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSub,itemID,new Object());
SP.UI.Notify.addNotification('Init Workflow: '+wfName+' on item: '+itemID, false);
}}});
Method 2- Using item URL & WF subscription ID
var wfName = "notification workflow";
var context = SP.ClientContext.get_current();
var web = context.get_web();
var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var itemUrl = 'http://MY_SITE/Test%20Lib/aaa.jpg';
var decodedUri = decodeURIComponent(itemUrl);
var wfSubs = wfServiceManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList('89fce79e-ced2-44c1-8ba0-9ea015a2d059');
context.load(wfSubs);
context.executeQueryAsync(function () {
wfsEnum = wfSubs.getEnumerator();
while (wfsEnum.moveNext()) {
var wfSub = wfsEnum.get_current();
if (wfSub.get_name() === wfName) {
var subscriptionID = "{" + wfSub.get_id() + "}";
StartDocumentWorkflow(subscriptionID, decodedUri);
SP.UI.Notify.addNotification('Init Workflow: '+wfName+' on item: '+itemID, false);
}}});
function StartDocumentWorkflow(templateID, itemUrl) {
$().SPServices({
operation: "StartWorkflow",
item: itemUrl,
templateId: templateID,
workflowParameters: "<root />",
completefunc: function () {
alert('Workflow started');
}
});
}
I use the following tags on page:
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/>
<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>
<script type="text/javascript" src="../../../Scripts/jquery.SPServices-0.7.1a.js"></script>
In both approachs, I get the Sp notification. In the 2nd method the success alert appears as well. However when i check the targeted document item, no workflow has been started.
Can anyone help me to figure out any mistakes or provide a JavaScript Object Model solution as i am looking for a solution in JSOM ?