Workflow REST API: Retrieve Task List
This sample shows how to retrieve a list of the authenticated user's workflow tasks (also known as the task list or worklist). Note the use of DataContract classes to serialize the task list and the tasks within the task list.
public static void Retrieve_Worklist_RESTAPI() {
//shows how to retrieve worklist tasks for a user
//you should be familiar with web request authentication and JSON to use this API
//define the URI and URL for the workflow tasks endpoint
//URL for the K2 server
string K2ServerURL = "https://k2.denallix.com";
string worklistTasksEndpointURI = @ "/Api/Workflow/V1/tasks";
string tasklistURL = K2ServerURL + worklistTasksEndpointURI;
//set up client and credentials
//we use static windows credentials here for brevity, see authentication samples for other auth mechanisms
string userID = "administrator@denallix.com";
string pwd = "K2pass!";
System.Net.WebClient webclient = new System.Net.WebClient();
webclient.Credentials = new System.Net.NetworkCredential(userID, pwd);
//retrieve the authenticated user's task list as JSON
string response = webclient.DownloadString(tasklistURL);
//process the JSON response using a JSON deserializer.
//In this case the built-in .NET DataContractSerializer class (you may want to use JSON.NET instead)
//instantiate the DataContract used to parse the returned JSON worklist
WorkflowRestAPI.Tasklist.K2TaskList tasklist = new WorkflowRestAPI.Tasklist.K2TaskList();
//Deserialize the response into the tasklist object, using the K2TaskList data contract.
//see below this code snippet for example of the data contract
using(System.IO.MemoryStream memstream = new MemoryStream(Encoding.UTF8.GetBytes(response))) {
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(tasklist.GetType());
memstream.Position = 0;
tasklist = (WorkflowRestAPI.Tasklist.K2TaskList) serializer.ReadObject(memstream);
}
//do something with the collection of tasks in the retrieved task list
foreach(WorkflowRestAPI.Tasklist.K2TaskListTask task in tasklist.K2Tasks) {
Console.WriteLine("Folio: " + task.WorkflowInstanceFolio);
Console.WriteLine("Workflow: " + task.WorkflowName);
Console.WriteLine("Step: " + task.ActivityName);
Console.WriteLine("Form URL: " + task.FormURL);
Console.WriteLine("**************");
}
}
// Tasklist/worklist data contract for the /tasks endpoint
[System.Runtime.Serialization.DataContract]
public class K2TaskList {
[DataMember(Name = "itemCount")]
public long ItemCount { get; set; }
[DataMember(Name = "tasks")]
public List < K2TaskListTask > K2Tasks { get; set; }
}
// Task object defined as it comes back for the K2 Tasklist call.
[System.Runtime.Serialization.DataContract]
public class K2TaskListTask {
[DataMember(Name = "instruction")]
public string Instruction { get; set; }
[DataMember(Name = "activityName")]
public string ActivityName { get; set; }
[DataMember(Name = "activityInstanceDestinationID")]
public long ActivityInstanceDestinationID { get; set; }
[DataMember(Name = "actions")]
public K2TaskListTaskActions Actions { get; set; }
[DataMember(Name = "activityInstanceID")]
public long ActivityInstanceID { get; set; }
[DataMember(Name = "eventName")]
public string EventName { get; set; }
[DataMember(Name = "eventDescription")]
public string EventDescription { get; set; }
[DataMember(Name = "formURL")]
public string FormURL { get; set; }
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "workflowDisplayName")]
public string WorkflowDisplayName { get; set; }
[DataMember(Name = "priority")]
public long Priority { get; set; }
[DataMember(Name = "originator")]
public K2TaskListOriginator Originator { get; set; }
[DataMember(Name = "serialNumber")]
public string SerialNumber { get; set; }
[DataMember(Name = "viewFlowURL")]
public string ViewFlowURL { get; set; }
[DataMember(Name = "taskStartDate")]
public string TaskStartDate { get; set; }
[DataMember(Name = "workflowCategory")]
public string WorkflowCategory { get; set; }
[DataMember(Name = "workflowInstanceFolio")]
public string WorkflowInstanceFolio { get; set; }
[DataMember(Name = "workflowID")]
public long WorkflowID { get; set; }
[DataMember(Name = "workflowInstanceID")]
public long WorkflowInstanceID { get; set; }
[DataMember(Name = "workflowName")]
public string WorkflowName { get; set; }
}
[DataContract]
public class K2TaskListTaskActions {
[DataMember(Name = "nonBatchableActions")]
public List < object > NonBatchableActions { get; set; }
[DataMember(Name = "batchableActions")]
public List < string > BatchableActions { get; set; }
[DataMember(Name = "systemActions")]
public List < string > SystemActions { get; set; }
}
[DataContract]
public class K2TaskListOriginator {
[DataMember(Name = "email")]
public string Email { get; set; }
[DataMember(Name = "manager")]
public string Manager { get; set; }
[DataMember(Name = "displayName")]
public string DisplayName { get; set; }
[DataMember(Name = "fqn")]
public string Fqn { get; set; }
[DataMember(Name = "username")]
public string Username { get; set; }
}
//shows how to retrieve worklist tasks for a user
//you should be familiar with web request authentication and JSON to use this API
//define the URI and URL for the workflow tasks endpoint
//URL for the K2 server
string K2ServerURL = "https://k2.denallix.com";
string worklistTasksEndpointURI = @ "/Api/Workflow/V1/tasks";
string tasklistURL = K2ServerURL + worklistTasksEndpointURI;
//set up client and credentials
//we use static windows credentials here for brevity, see authentication samples for other auth mechanisms
string userID = "administrator@denallix.com";
string pwd = "K2pass!";
System.Net.WebClient webclient = new System.Net.WebClient();
webclient.Credentials = new System.Net.NetworkCredential(userID, pwd);
//retrieve the authenticated user's task list as JSON
string response = webclient.DownloadString(tasklistURL);
//process the JSON response using a JSON deserializer.
//In this case the built-in .NET DataContractSerializer class (you may want to use JSON.NET instead)
//instantiate the DataContract used to parse the returned JSON worklist
WorkflowRestAPI.Tasklist.K2TaskList tasklist = new WorkflowRestAPI.Tasklist.K2TaskList();
//Deserialize the response into the tasklist object, using the K2TaskList data contract.
//see below this code snippet for example of the data contract
using(System.IO.MemoryStream memstream = new MemoryStream(Encoding.UTF8.GetBytes(response))) {
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(tasklist.GetType());
memstream.Position = 0;
tasklist = (WorkflowRestAPI.Tasklist.K2TaskList) serializer.ReadObject(memstream);
}
//do something with the collection of tasks in the retrieved task list
foreach(WorkflowRestAPI.Tasklist.K2TaskListTask task in tasklist.K2Tasks) {
Console.WriteLine("Folio: " + task.WorkflowInstanceFolio);
Console.WriteLine("Workflow: " + task.WorkflowName);
Console.WriteLine("Step: " + task.ActivityName);
Console.WriteLine("Form URL: " + task.FormURL);
Console.WriteLine("**************");
}
}
// Tasklist/worklist data contract for the /tasks endpoint
[System.Runtime.Serialization.DataContract]
public class K2TaskList {
[DataMember(Name = "itemCount")]
public long ItemCount { get; set; }
[DataMember(Name = "tasks")]
public List < K2TaskListTask > K2Tasks { get; set; }
}
// Task object defined as it comes back for the K2 Tasklist call.
[System.Runtime.Serialization.DataContract]
public class K2TaskListTask {
[DataMember(Name = "instruction")]
public string Instruction { get; set; }
[DataMember(Name = "activityName")]
public string ActivityName { get; set; }
[DataMember(Name = "activityInstanceDestinationID")]
public long ActivityInstanceDestinationID { get; set; }
[DataMember(Name = "actions")]
public K2TaskListTaskActions Actions { get; set; }
[DataMember(Name = "activityInstanceID")]
public long ActivityInstanceID { get; set; }
[DataMember(Name = "eventName")]
public string EventName { get; set; }
[DataMember(Name = "eventDescription")]
public string EventDescription { get; set; }
[DataMember(Name = "formURL")]
public string FormURL { get; set; }
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "workflowDisplayName")]
public string WorkflowDisplayName { get; set; }
[DataMember(Name = "priority")]
public long Priority { get; set; }
[DataMember(Name = "originator")]
public K2TaskListOriginator Originator { get; set; }
[DataMember(Name = "serialNumber")]
public string SerialNumber { get; set; }
[DataMember(Name = "viewFlowURL")]
public string ViewFlowURL { get; set; }
[DataMember(Name = "taskStartDate")]
public string TaskStartDate { get; set; }
[DataMember(Name = "workflowCategory")]
public string WorkflowCategory { get; set; }
[DataMember(Name = "workflowInstanceFolio")]
public string WorkflowInstanceFolio { get; set; }
[DataMember(Name = "workflowID")]
public long WorkflowID { get; set; }
[DataMember(Name = "workflowInstanceID")]
public long WorkflowInstanceID { get; set; }
[DataMember(Name = "workflowName")]
public string WorkflowName { get; set; }
}
[DataContract]
public class K2TaskListTaskActions {
[DataMember(Name = "nonBatchableActions")]
public List < object > NonBatchableActions { get; set; }
[DataMember(Name = "batchableActions")]
public List < string > BatchableActions { get; set; }
[DataMember(Name = "systemActions")]
public List < string > SystemActions { get; set; }
}
[DataContract]
public class K2TaskListOriginator {
[DataMember(Name = "email")]
public string Email { get; set; }
[DataMember(Name = "manager")]
public string Manager { get; set; }
[DataMember(Name = "displayName")]
public string DisplayName { get; set; }
[DataMember(Name = "fqn")]
public string Fqn { get; set; }
[DataMember(Name = "username")]
public string Username { get; set; }
}