K2 REST Services: JavaScript Samples
This topic provides some samples that demonstrate how to interact with the Workflow REST services through JavaScript.
Starting a new workflow with the REST web service using javascript
function startProcess(folio) {
var currentTime = newDate()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var secs = currentTime.getSeconds()
var date = month + "" + day + "" + hours + "" + minutes + "" + secs;
folio = folio + "_" + date;
$.ajax({
url: "http://[k2server]/k2services/REST.svc/Process/Definitions([ProjectName]_B_[WorkflowName])/StartInstance?folio=" + folio,
method: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
async: true,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
crossDomain: false,
error: alert('error'), //handle Error
success: alert('success') //handle Success
});
}
Retrieving a worklist item with the REST web service and javascript
function getK2WorklistItemBySN(SN) {
$.ajax({
url: "http://[k2server]/k2services/REST.svc/Worklist/Items(" + SN + ")?piDataField=true&actXmlField=true&$format=json",
method: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
async: true,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
crossDomain: false,
error: alert('error'), //handle Error
success: alert('success') //handle Success
});
}
Completing a worklist item using the REST service and javascript
function actionK2WorklistItem(SN, k2Action) {
$.ajax({
url: "http://[k2server]/k2services/REST.svc/Worklist/Items(" + SN + ")/Actions(" + k2Action + ")/Execute?$format=json",
method: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
async: true,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
crossDomain: false,
error: alert('error'), //handle Error
success: alert('success') //handle Success
});
}