How to get workflow template id programmatically using JavaScript Object Model (jsom) in SharePoint

This SharePoint workflow tutorial explains, how to get a workflow template id programmatically using the javascript object model (jsom) in SharePoint Online or SharePoint 2013/2016.


Workflows are an integral part of SharePoint. We can use workflows to automate various business processes in SharePoint 2013 or SharePoint Online. We can develop workflows using a browser, SharePoint designer or by using visual studio 2015/2017/2019.


We can develop two types of workflows:

  • List workflow: These workflows will be attached to SharePoint online list or document libraries.

  • Site workflow: This workflow is not for any specific list or library, these will be available for sites.

Here in this SharePoint workflow tutorial, we will discuss how we can get workflow template id programmatically using JavaScript Object model (jsom). Here we have developed one site workflow using visual studio 2015 as a SharePoint hosted add-in and we have attached to the host website.


SharePoint get workflow template id programmatically using jsom


I have added a simple button and on click of that, it will display the workflow id. You can add the below code inside a script editor web part inside a web part page in SharePoint online site.


Here we have the workflow name as “TestWorkflow” and through the below code we can retrieve the workflow id from SharePoint online site.

<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() {
var clientContext = new SP.ClientContext.get_current();
this.workflows = clientContext.get_web().get_workflowAssociations();
clientContext.load(this.workflows);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
};
function onQuerySucceeded(sender, args) {
var enumerator = this.workflows.getEnumerator();
while(enumerator.moveNext())
{
var workflow = enumerator.get_current();
if(workflow.get_name() == "TestWorkflow")
alert(workflow.get_id());
}
};
function onQueryFailed(sender, args) {
alert("Error");
};
</script>

This is how we can get the SharePoint 2013 workflow template id using jsom. Hope this will be helpful.


Source: Opensource.com

Recent Posts

See All