如何使用按钮 (javascript?) 激活 SharePoint 2013 工作流

How do I activate a SharePoint 2013 workflow with a button (javascript?)

早上好。我是编码世界的新手,所以我的技能每天都在增长。我正在尝试使用按钮和 Javascript 激活 SharePoint 2013 列表工作流。我知道有很多可用的示例,老实说,我对无法自己解决这个问题感到有点尴尬。然而,我迄今为止看到的所有代码都包含启动变量,但我的工作流程没有。我不知道如何更改示例以排除启动变量。请帮忙。我看过的示例代码:

http://ranaictiu-technicalblog.blogspot.com/2013/06/sharepoint-2013-start-workflow-with.html https://www.codeproject.com/Articles/607127/Using-SharePoint-2013-Workflow-Services-JS-API#example5 https://sharepoint.stackexchange.com/questions/236329/start-sharepoint-designer-workflow-2013-using-javascript

我首先使用了以下代码 link:

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

//Subscription id - Workflow subscription id
//list item id for which to start workflow. If site workflow, then send null for itemId

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');
   ctx.executeQueryAsync(
       function (sender, args) {
           var params= new Object();
           //Find initiation data to be passed to workflow.
           var formData = subscription.get_propertyDefinitions()["FormData"];
           if (formData != null && formData != 'undefined' && formData != "") {
               var assocParams = formData.split(";#");
               for (var i = 0; i < assocParams.length; i++) {
                   params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]];
               }
           }
           if (itemId) {
               wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);
           }
           else {
               wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);
           }
           ctx.executeQueryAsync(
               function (sender, args) {
                   closeInProgressDialog();
               },
               function (sender, args) {
                   closeInProgressDialog();
                   alert('Failed to run workflow');
               }
           );
       },
       function (sender, args) {
           closeInProgressDialog();
           alert('Failed to run workflow');
       }
   );
 }

function closeInProgressDialog() {
   if (dlg != null) {
       dlg.close();
   }
}
 

function showInProgressDialog() {
   if (dlg == null) {
       dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait...", "Waiting for workflow...", null, null);
   }
}

并插入以下内容 HTML 以创建按钮。

<button onclick="function StartWorkflow('8E645164-959C-4358-B22C-47FDA93F7906',5)">Click Me</button>

我 100% 确信我的 subscriptionId 和 itemId 是正确的。

请帮忙。

谢谢。

我的测试代码供您参考,与您的代码略有不同:

<input type="button" id="test" value="Click Me" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="/_layouts/15/sp.workflowservices.js"></script>    
<script>
  $("#test").click(function(){
    console.log(1)
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', init);
  function init() {
    $.getScript(SP.Utilities.Utility.getLayoutsPageUrl('sp.js'), function () {
      $.getScript(SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'), function () {
        StartWorkflow("SubscriptionId","itemid","siteurl");
      });
    });
  }
  //dialog element to show during processing
  var dlg = null;
  //Subscription id – Workflow subscription id
  //list item id for which to start workflow. If site workflow, then send null for itemId
  //SiteURL – site collection where the workflow exists
  function StartWorkflow(subscriptionId, itemId, SiteURL) {
    showInProgressDialog();
    var ctx = new SP.ClientContext(SiteURL);
    var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
    var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);
    ctx.load(subscription, 'PropertyDefinitions');
    ctx.executeQueryAsync(
      function (sender, args) {
        var params = new Object();
        //Find initiation data to be passed to workflow.
        var formData = subscription.get_propertyDefinitions()["FormData"];
        if (formData != null &&   formData != 'undefined' &&   formData != "") {
      var assocParams = formData.split(";#");
      for (var i = 0; i < assocParams.length; i++) {
        params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]];
      }
    }
    if (itemId) {
      wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);
    }
    else {
      wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);
    }
    ctx.executeQueryAsync(
      function (sender, args) {
        closeInProgressDialog(SiteURL);
      },
      function (sender, args) {
        closeInProgressDialog(SiteURL);
        $('#msg')[0].innerHTML = "Woops – something went wrong, the document may have already been reviewed";
      }
    );
  },
  function (sender, args) {
    closeInProgressDialog(SiteURL);
    $('#msg')[0].innerHTML = "Woops – something went wrong, the document may have already been reviewed";
  }
);
}

  function closeInProgressDialog(SiteURL) {
    if (dlg != null) {
      dlg.close();
      $('#msg')[0].innerHTML = "Thank you – you can now close the page";
    }
  }
  function showInProgressDialog() {
    if (dlg == null) {
      dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait…", "Waiting for workflow…", null, null);
    }
  }
  })
  
</script>