How to Trigger Sharepoint 2013 Designer workflow on a list item by calling REST API using jQuery?

sharepoint 2013 start workflow programmatically javascript
InfoPath alternatives for form designing SharePoint

Introduction:
In this article we learn how to trigger the Sharepoint 2013 designer list Workflow on a list item using REST API and JQuery. In SharePoint 2013 designer workflow, we uncheck the workflow triggering mechanism on list item being created or updated. This was done in an effort to prevent recursive calls to the workflow initiation.

call sharepoint designer workflow using rest api
call sharepoint designer workflow using rest api

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: The Start Workflow Automatically When an item is Created/Changed that are performed 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 that we want to “Trigger the Sharepoint 2013 list workflow” on any particular item and send email to user as per need.

sharepoint 2013 start workflow rest api
sharepoint 2013 start workflow rest api

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 to Trigger Workflow button.
Use the procedure given below.

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

trigger workflow using rest api sharepoint 2013
trigger workflow using rest api sharepoint 2013

In the following code. We fetch the “subscriptionId” in the code as per Your 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 Web Parts picker area, go to the Media and Content category, select the Script Editor Web Part and press the Add button.

Read some SharePoint 2013 tutorials on:

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.




<script type=”text/javascript” src=”/immlcs/SiteAssets/Script/jquery-1.10.2.js”></script>

<script type=”text/javascript”>

$(document).ready(function ($) {

ItemIdDropDownBind();

$(“#StartWorkflow”).click(function () { StartWF() });

});

//StartWorkflow

function StartWF() {

$.ajax({

url: _spPageContextInfo.siteAbsoluteUrl + “/_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId=’BB20B816-2AEF-4299-B6BF-43910578BA8F’,itemId=’ ” + $(“#drpItem option:selected”).text() + “‘)”,

type: “POST”,

contentType: “application/json;odata=verbose”,

headers: {

“Accept”: “application/json;odata=verbose”,

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()

},

success: function (data) {

alert(‘Workflow Trigger Successfully’);

},

error: function (data) {

alert(“Error”);

}

});

}

function ItemIdDropDownBind() {

var url = _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getbytitle(‘TEST’)/items?$orderby=ID asc&$top=5000″;

getListItems(url, function (data) {

var items = data.d.results;

var inputItemIDElement = ‘<select id=”drpItem” name=”options”><option value=””></option>’;

for (var i = 0; i < items.length; i++) {

var itemId = items[i].ID,

itemVal = items[i].ID;

inputItemIDElement += ‘<option value=”‘ + itemVal + ‘”selected>’ + itemId + ‘</option>’;

}

inputItemIDElement += ‘</select>’;

$(‘#ItemID’).append(inputItemIDElement);

}, function (data) {

alert(“An error occurred. Please try again.”);

});

}

function getListItems(siteurl, success, failure) {

$.ajax({

url: siteurl,

method: “GET”,

headers: { “Accept”: “application/json; odata=verbose” },

success: function (data) {

success(data);

},

error: function (data) {

failure(data);

}

});

}

</script>

Final out Put:

Select Item Id and click Trigger workflow button

sharepoint 2013 start workflow programmatically javascript
sharepoint 2013 start workflow programmatically javascript
Start a workflow with SharePoint 2013 using Rest API jQuery
Start a workflow with SharePoint 2013 using Rest API jQuery

Trigger Workflow Email O/P:

sharepoint 2013 start workflow javascript
sharepoint 2013 start workflow javascript

Hope this article will be helpful to trigger workflow using Rest API in SharePoint 2013 for list item.

Similar SharePoint 2013 Tutorials


About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply