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.

//use a System.Net.Http.HttpClient to send the request.
//TODO: You will have to construct the HttpClient and authentication for the webclient before running the snippet
System.Net.Http.HttpClient k2WebClient;

//construct the endpoint for the tasklist operation. Your URL will likely be different.
string operationEndPoint = @ "https://k2.denallix.com/api/workflow/v1/tasks"

//retrieve the authenticated user's task list as JSON
string response = k2WebClient.GetStringAsync(operationEndPoint ).Result;

//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
WorkflowRestAPISamples.Tasks_TasklistContract.K2TaskList tasklist = new WorkflowRestAPISamples.Tasks_TasklistContract.K2TaskList();

//Deserialize the response into the tasklist object, using the K2TaskList 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 = (WorkflowRestAPISamples.Tasks_TasklistContract.K2TaskList) serializer.ReadObject(memstream);
}
//do something with the collection of tasks in the retrieved task list
foreach (WorkflowRestAPISamples.Tasks_TasklistContract.K2TaskListTask task in tasklist.K2Tasks) {
 Console.WriteLine ("Folio: " + task.WorkflowInstanceFolio);
 Console.WriteLine ("Serial Number: " + task.SerialNumber);
}

//DataContract for the task list and tasks within the task list
namespace WorkflowRestAPISamples.Tasks_TasklistContract {
// 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; }
}
}