K2 REST Services: JavaScript Samples

This content applies to legacy components (such as K2 Studio and K2 for Visual Studio), legacy assemblies, legacy services or legacy functionality. If you have upgraded from K2 blackpearl 4.7 to K2 Five, these items may still be available in your environment. These legacy items may not be available in new installations of K2 Five. These legacy items may also not be available, supported, or behave as described, in future updates or versions of K2. Please see the legacy component support policy for more information about support for these components.

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
 });
}