0

我有一個要求通過JavaScript啓動SP工作流。爲此,我寫了一些代碼行來啓動工作流。但由於未定義的對象,我得到了一個訂閱失敗錯誤。這發生在ExcecuteasyncQuery方法中。所以我沒有得到哪個對象返回未定義的值。我的代碼如下所示。SharePoint工作流訂閱服務失敗

<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> 
<script type="text/javascript"> 
var subID = "3debdbad-db52-4586-87e1-40e4db581da5"; 

function GetCurrentItemID() 
{ 
var ctx = SP.ClientContext.get_current(); 
var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx); 

for (item in selectedItems) 
{ 
var itemId = selectedItems[item].id; 
startWorkflow(itemId, subID); 
} 
} 

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){ 
     alert("Subscription load success. Attempting to start workflow.");   
     var inputParameters = {}; 
     wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem 
     (subscription, itemID, inputParameters); 

     context.executeQueryAsync(
      function(sender, args){ alert("Successfully starting workflow."); }, 
      function(sender, args){ 
       alert("Failed to start workflow."); 
       alert("Error: " + args.get_message() + "\n" + args.get_stackTrace()); 
      } 
     ); 
    }, 
    function(sender,args){ 
     alert("Failed to load subscription."); 
     alert("Error: " + args.get_message() + "\n" + args.get_stackTrace()); 
    } 
); 
} 
</script> 

我在點擊按鈕時調用GetCurrentItemID()函數。但在那個時候,我收到以下錯誤。

無法加載訂閱

無效請求

未定義

可以在任何一個請幫我理清這個問題。提前致謝。

回答

2

根據劇本和錯誤信息,

我建議你勾選「訂閱」和「的itemId」對象是否擁有正確的價值觀。

更重要的是,你可以在下面的兩個鏈接瞭解如何使用JavaScript客戶端對象模型啓動工作來看一看:

http://usamawahabkhan.blogspot.com/2013/07/start-workflow-sharepoint-2010.html

https://sharepoint.stackexchange.com/questions/87015/solved-start-a-workflow-of-the-host-web-via-sharepoint-app-and-jsom

隨時對測試結果回覆或者如果有任何進展。

+0

itemId和is返回正確的值並且訂閱返回非空值。 – MAC

+0

生病回覆你。我正在尋找這件事。 –

+0

我們可以做Skype文字嗎? – MAC

0

基於他人提供的解決方案,下面是我能夠使用的代碼。注意:您需要將工作流程設置爲「允許手動啓動此工作流程」。

/** 
* Starts a SharePoint 2013 Workflow on a particular list item. 
* Params: 
*  workflowName: The name of the Workflow 
*  listGUID: the GUID of the list 
*  itemId: the ID of the list item 
*/ 
function startListWorkflow(workflowName, listGUID, itemId){ 
    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", 
      function(){ 
       var ctx = new SP.ClientContext.get_current(), 
       wfsManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()), 
       wfSubs = wfsManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(listGUID); 
       ctx.load(wfSubs); 
       ctx.executeQueryAsync(function() { 
        wfsEnum = wfSubs.getEnumerator(); 
        while (wfsEnum.moveNext()) { 
         var wfSub = wfsEnum.get_current(); 
         if (wfSub.get_name() === workflowName) { 
          var initiationParams = {}; 
          wfsManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSub, itemId, initiationParams); 
          ctx.executeQueryAsync(
           function (sender, args) { 
            console.log("Successfully started workflow."); 
           }, 
           function (sender, args) { 
            console.log("Failed to start the workflow."); 
            console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace()); 
           } 
          ); 
         } 
        } 
       },function(e){ 
        console.error(e) 
       }); 
      } 
     ); 
    });   
}