Sunday, June 21, 2015

K2 BlackPerl REST API : Get Count Of Tasks Assigned To User.

Scenario -
In K2 worklist webpart, it lists the tasks assigned to user but not the count or number of task assigned to user. It would be very useful for the users to know how many tasks are pending to action at a glance. 
Our goal is to get the number of tasks assigned to user for particular K2 application.

K2 REST Services -
K2 Services REST endpoints provide standard URIs for interacting with the available data and operations. 

There are five REST service provided by K2, for now.
  1. Core Service - The Core service is useful for ensuring a connection can be established to the services and determining the current user context making the call.
  2. Identity Service - The Identity service is useful for finding users that the K2 Server has access to; for example, when the user wishes to redirect an item to another user.
  3. Task Service - The Task service is useful for determining what outstanding tasks a user has, as well as batch-actioning tasks that a user might have actioned while they were offline.
  4. Worklist service - The Worklist service is useful for interacting with worklist items.
  5. Process Service - The Process service is useful for interacting with process definitions and process instances

K2 has good documentation on these services with example.

Back to our main business - 
I used the ajax call to the Task services and used the length function to get the tasks count.
It's important to use "Cache:false" parameter in ajax call as in IE it cache the count number, so each-time when page refresh it should get the correct count number.

You can filter the number of tasks to be return for particular process/for all processes which are inside particular folder.

Here is how you can define the filter using the 'filterXml' as query string parameter

url: 'http://[server:port]/K2Services/SyncREST.svc/Task/Items?filterXml=<Criteria xmlns=""><Filter Field="ProcessFolder" Comparison="Equal" ValueType="String">[solution/Project name]</Filter></Criteria>'

When you deploy the K2 project, in workspace it becomes a folder and you can expand it to see all processes in that solution/project.

In my case, I had to get the count for all the processes inside the folder for a user.

There are good number of options available to filter the tasks, for information please check the K2 documentation.

Finally, here is working code which gives the number of tasks assigned in all process in a particular folder.

<script src=""></script>
var tasks;
function GetMyTasksCount() {
        // Replace the [server:port] and filter criteria [solution/Project name] as per your requirement
        url: 'http://[server:port]/K2Services/SyncREST.svc/Task/Items?filterXml=<Criteria xmlns=""><Filter Field="ProcessFolder" Comparison="Equal" ValueType="String">[solution/Project name]</Filter></Criteria>',
        method: 'GET',
        crossDomain: false,
cache: false,
        error: function (data, error, status) {
$("p:contains('My Tasks')").append(" "+"(cannot retrieve count)");
        success: function (data) {
            tasks = data;

function ShowMyTasksCount() {        
    $("p:contains('MY TASKS')").append(" "+"(" +$(tasks).find("Task").length+")");

/* Get the worklist when the page loads */
$(document).ready(function () {

<p>MY TASKS</p>


  1. Why use set crossdomain:false ? It didnt work on sharepoint page, because k2 installed on different domain

  2. Access is denied. Error why?
    I have tried with admin user but every time its showing Access is denied.

    1. Make sure you have setup SPN for service account.