Question

Using the Workflow REST API in JavaScript

  • 20 January 2020
  • 3 replies
  • 201 views

Has anyone migrated from the Worklist.sdk to the REST API ?

 

We use the Worklist.sdk in Javascipt within a form. We have upgraded from K2 4.7 to 5.2 and can no longer use the Worklist.sdk. I have looked at the K2 documentation and I can not understand how to use the REST API in my JS.

 

Any help or examples greatly appreciated. My existing JS code is shown below

 

Thanks

 

Kevin

 

We use the following JS to get a list of tasks, and then iterate through our list views, using folio value, so the rows with associated tasks go pink containing hyperlinks to the task.

 

< script >

var worklistProcessed = false;

function callWorklistService() {
if (worklistProcessed) {
return;
}

var environmentCode = document.domain.substr(0, 2);
var URL = "https://" + environmentCode + "-k2runtime.northumbria.ac.uk/Runtime/Runtime/Form/My+Documents/Worklist.sdk?" + Date.now();
jQuery.ajax({
type: "GET",
url: URL,
dataType: "xml",
contentType: "application/xml; charset=utf-8",
success: function(data) {
var xmlDoc;
if ($(data).find("WorklistItemCollection").length == 0) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(data.xml);
} else {
xmlDoc = data;
}

$($("table.grid-content-table").slice(0, 4).find("tr").get().reverse()).each(function() {
var submissionID = $(this).find("td:first").text().trim();
if (submissionID.indexOf("No items to display") > -1) {
return;
}
var matchingTaskURL = $(xmlDoc).find("ProcessInstance[Folio='" + submissionID + "']").closest("WorklistItem").find("Data").text();
if (matchingTaskURL != null && matchingTaskURL != "") {
$(this).attr("onclick", "window.location.href='" + matchingTaskURL + "'; showSpinner(); if (event.stopPropagation) { event.stopPropagation(); } event.cancelBubble = true; ");
$(this).removeClass("even");
$(this).attr("style", "background-color:#ffb3b3");
$(this).find("td").attr("style", "background-color:#ffb3b3");
if ($(this).prevAll().not("[style='background-color:#ffb3b3']").length !== 0) {
$(this).insertBefore($(this).closest("table").find("tr").not("[style='background-color:#ffb3b3']").first());
}

});
worklistProcessed = true;
}
});

}
callWorklistService(); < /script>

 

 


3 replies

Badge +13

Hi,


 


There is an example in the offical dev document, but first you need to enable the Workflow REST API. The example can be found here. To determine if the Workflow REST API can be used properly, you can use a tool such as Postman to test out the REST API first.


 

Thanks for your reply. We seem to have a problem ysing ADFS - I am talking to K2 about this.

 

Can you advise, how is it possible to get the password of the current user into the javascript, as in the example K2 code below?

 

beforeSend: function(xhr) {
   xhr.setRequestHeader('Authorization''Basic ' + window.btoa(unescape(encodeURIComponent("[username]" + ':' + "[PASSWORD]"))));
   xhr.setRequestHeader("Content-Type""application/json; charset=UTF-8");
  },

It is a totally wrong move.

 

You don't ever retrieve the password using the AJAX request from the server.

 

By retrieving the password in the clientside, you are opening the doors for malicious actors.

 

I don't know why you need a password on the clientside but whatever you need, don't fetch the password.

If you want, you can send a request to get the current authenticated user's username or id.

 

The way ajax works is that you will send a network request to the server without page refresh and get the data from the database in JSON format and using client-side javascript function like JSON.parse(), you will convert it into a JavaScript object and display the data into the frontend.

 

Best Regards,

Reply