How to get workflow template id using javascript in SharePoint

This SharePoint workflow tutorial, we will discuss how to get workflow template id using the javascript object model (jsom) in SharePoint 2013/2016 or SharePoint Online?


Workflows are very useful in SharePoint. We can automate the business process using Workflows in SharePoint 2013 or SharePoint Online. We can develop a workflow using out of box feature like browser, SharePoint 2013 designer or we can develop workflow by using visual studio 2015. Once developed and deployed a workflow, we can attach to a list or library or we can use a site workflow.


If a workflow is attached to a SharePoint online or SharePoint 2013 list, we can retrieve workflow template id from a list using JavaScript object model code (jsom).


Here we have a list in our SharePoint online site and various workflows are attached to the list. By using the below code we are going to retrieve the id of the workflow whose name is “Archiving”.


JSOM Code to get workflow template id in SharePoint Online/2013


In this particular example, I have added the below code inside a script editor web part which I have added inside a web part page in SharePoint Online.

<input type="button" id="btnSubmit" value="Get Workflow ID" /><br/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
bindButtonClick();
});
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
getWorkflowId();
});
}
function getWorkflowId() {
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 () {
context = SP.ClientContext.get_current();
web = context.get_web();
workflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager(context, web);
var subs = workflowServicesManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList('4C384857-8AEF-484E-8163-FED3592E15A4');
context.load(subs);
context.executeQueryAsync(function () {
var subEnumerator = subs.getEnumerator();
while (subEnumerator.moveNext()) {
var sub = subEnumerator.get_current();
if (sub.get_name() == 'Archiving') {
var templateId = 'Workflow id: '+sub.get_id();
alert(templateId)
return;
}
}
},
function (sender, args) {
alert(args.get_message());
});
});
});
}
</script>

Once you Save the code and click on the button, it will display the workflow id inside an alert box like below:

Get workflow template id using jsom sharepoint 2013


I hope this tutorial will be helpful to get workflow template id using jsom (JavaScript object model) in SharePoint Online.


Source: SP Guide