0

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 ?

0

http://procentrix.com/Community/Rajya/post.aspx?ID=7 and it looks like he has some extra steps you have not taken

As a follow on, I would like to bring your attention to this. One of the best works on JSOM with workflows (note that the code uses a C# wrapper to build out his .js) http://code.msdn.microsoft.com/sharepoint-2013-workflow-050f5211

Content from the first link for posterity with full credit to Rajya

How to start SharePoint 2013 workflow using JSOM

SharePoint Designer Workflow Start options can be manual, or on create or on change.

If Designer workflow setting is on change, it triggers for any change on the item.

I know you can check inside workflow and if criteria doesn't match, end the workflow but is still little annoying

If you have a workflow which needs to run only when Status = 'something'.

Here I will show you how to start a workflow on button click using JSOM, so no need to worry about triggering workflow on every change where there is no need.

Set your workflow start settings to Manual.

Add these references to your HTML page

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){

SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));

SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager");

});

Create a button on the HTML page and copy this script to button click event or page load.

var definitionId = '{98832FBE-BA83-4A19-AAC5-F947A2131BB5}';

definitionId is the workflow subscription id which you will get when you go to list settings -> workflow settings -> click on your workflow - you will see SubscriptionId={98832FBE-BA83-4A19-AAC5-F947A2131BB5} in the URL, copy GUID.

var context = SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
context.load(servicesManager);
var subs = servicesManager.getWorkflowSubscriptionService().getSubscription(definitionId);
context.load(subs);
var initiationParams = {};
servicesManager.getWorkflowInstanceService().startWorkflowOnListItem(subs, ListItemId, initiationParams);

ListItemId is the id of the item on which you need to trigger workflow.

context.executeQueryAsync(function (sender, args) {
console.log('Workflow started.');
}, errFunc);
function errFunc(sender, args)
{
alert("Error occured! " + args.get_message() + '\r\nStack trace: ' + args.get_stackTrace());
}
  • Nice answer. Actually, you need to load only subs. The web and the serviceManager are not reauired to be loaded. Furthermore they are not loaded at all until you don't call executeQueryAsync. – Emaborsa Mar 9 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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