How To Trigger SharePoint 2013 Designer Workflow On A List Item By Calling REST API Using jQuery

Introduction

In SharePoint 2013 designer workflow, we uncheck the workflow triggering mechanism on a list item being created or updated. This is done in an effort to prevent the recursive calls to the workflow initiation.


Sharepoint

However, even though it takes quite a few workflow Start options, it is still possible to start the workflow manually from within the calling workflow. The following Option does the trick: start workflow automatically when an item is Created/Changed in Workflow Designer internally (automatically)

Scenario

I have created a custom list named “TEST” on the host site and have added multiple items. Now, let’s say we want to “Trigger the Sharepoint 2013 list workflow” on any particular item and send an email to the user as per our need.

Sharepoint

Objective

I wanted to get the item ID of the list item so that I could use it in my HTML to fetch the Item ID of the list item and bind to the drop-down. Once we have selected any Item ID from the list of Item IDs from the drop-down, click on the "Trigger Workflow" button.

Use the procedure given below.

I have created a simple Sharepoint designer workflow for sending an email. Given below is the created mail template.

Sharepoint

In the following code, we fetch the "subscriptionId” in the code as per our Workflow

Step 1

Navigate to your SharePoint 2013 site.

Step 2

From this page, select Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click "Web Part" option. In the Web Parts picker area, go to the "Media and Content" category, select the Script Editor Web Part, and press the "Add" button.

Step 3

Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert HTML and/or JavaScript, as shown below.

  1. <script type="text/javascript" src="../.../SiteAssets/Script/jquery-1.10.2.js"></script>  
  2.     <script type="text/javascript">  
  3.         $(document).ready(function ($) {  
  4.             ItemIdDropDownBind();  
  5.             $("#StartWorkflow").click(function () { StartWF() });  
  6.         });  
  7.   
  8.         //StartWorkflow  
  9.         function StartWF() {  
  10.             $.ajax({  
  11.                 url: _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId='BB20B816-2AEF-4299-B6BF-43910578BA8F',itemId=' " + $("#drpItem option:selected").text() + "')",  
  12.                 type: "POST",  
  13.                 contentType: "application/json;odata=verbose",  
  14.                 headers: {  
  15.                     "Accept""application/json;odata=verbose",  
  16.                     "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  17.                 },  
  18.                 success: function (data) {  
  19.                     alert('Workflow Trigger Successfully');  
  20.                 },  
  21.                 error: function (data) {  
  22.                     alert("Error");  
  23.                 }  
  24.             });  
  25.   
  26.         }  
  27.         function ItemIdDropDownBind() {  
  28.             var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('TEST')/items?$orderby=ID asc&$top=5000";  
  29.             getListItems(url, function (data) {  
  30.                 var items = data.d.results;  
  31.                   
  32.                 var inputItemIDElement = '<select id="drpItem" name="options"><option  value=""></option>';  
  33.   
  34.                 for (var i = 0; i < items.length; i++) {  
  35.                     var itemId = items[i].ID,  
  36.                      itemVal = items[i].ID;  
  37.                     inputItemIDElement += '<option value="' + itemVal + '"selected>' + itemId + '</option>';  
  38.                 }  
  39.   
  40.                 inputItemIDElement += '</select>';  
  41.   
  42.                 $('#ItemID').append(inputItemIDElement);  
  43.   
  44.   
  45.             }, function (data) {  
  46.                 alert("An error occurred. Please try again.");  
  47.             });  
  48.         }  
  49.         function getListItems(siteurl, success, failure) {  
  50.             $.ajax({  
  51.                 url: siteurl,  
  52.                 method: "GET",  
  53.                 headers: { "Accept""application/json; odata=verbose" },  
  54.                 success: function (data) {  
  55.                     success(data);  
  56.                 },  
  57.                 error: function (data) {  
  58.                     failure(data);  
  59.                 }  
  60.             });  
  61.         }  
  62.     </script>  

Final out Put

Select the Item Id and click "Trigger workflow" button.

Sharepoint

Sharepoint
Trigger Workflow Email O/P

Sharepoint

Similar Articles