Workflow REST API: Open and Complete Task

This sample shows how to open a specific task, allocate the task to the authenticated user, and finally complete the task with a specific task action. Note the use of DataContract classes to serialize the task.

public static void Open_Task_RESTAPI() {
 //shows how to open a specific task using the task serial number
 //NOTE: you should be familiar with web request authentication and JSON to use this API

 //define the URI and URL for the workflow task endpoint
 //URL for the K2 server
 string K2ServerURL = "https://k2.denallix.com";
 string worklistTasksEndpointURI = @ "/Api/Workflow/V1/tasks/";
 //TODO: provide the task serial number for the task you want to open.
 //You can get this value from the serialNumber property of a task from the task list endpoint
 string serialNumber = "14_23";
 //build up the URI for the task endpoint
 string taskURL = K2ServerURL + worklistTasksEndpointURI + serialNumber;


 //set up client and credentials for the web request
 //we use static windows credentials here for brevity, see authentication samples for other auth mechanisms
 string userID = "administrator@denallix.com";
 string pwd = "K2pass!";
 NetworkCredential k2credentials = new NetworkCredential(userID, pwd);
 System.Net.Http.HttpClientHandler loginHandler = new System.Net.Http.HttpClientHandler(); {
  loginHandler.Credentials = k2credentials;
 };
 //instantiate the client that we will use to send the request
 System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(loginHandler, true);

 string responseBody = httpClient.GetStringAsync(taskURL).Result;
 WorkflowRestAPI.Task.K2Task task = new WorkflowRestAPI.Task.K2Task();
 using(System.IO.MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(responseBody))) {
  DataContractJsonSerializer ser = new DataContractJsonSerializer(task.GetType());
  task = ser.ReadObject(ms) as WorkflowRestAPI.Task.K2Task;
 }

 //Do something with the task information
 Console.WriteLine("Workflow Name" + task.WorkflowName);
 Console.WriteLine("Activity Name: " + task.ActivityName);
 Console.WriteLine("Folio: " + task.WorkflowInstanceFolio);
 foreach(string systemAction in task.Actions.SystemActions) {
  Console.WriteLine("System Action: " + systemAction);
 }
 foreach(string batchAction in task.Actions.BatchableActions) {
  Console.WriteLine("Batchable Action: " + batchAction);
 }
 foreach(string nonbatchAction in task.Actions.NonBatchableActions) {
  Console.WriteLine("Non-Batchable Action: " + nonbatchAction);
 }
 Console.WriteLine("*********");

 //opening (allocating) the task with the actions/assign endpoint
 string assignTaskURL = K2ServerURL + worklistTasksEndpointURI + serialNumber + @ "/actions/assign";
 //re-use the same httpclient to send the request
 System.Net.Http.StringContent allocateTaskHttpContent = new System.Net.Http.StringContent("{}", Encoding.UTF8, "application/json");
 var allocateResult = httpClient.PostAsync(assignTaskURL, allocateTaskHttpContent).Result;
 //do something with the result, if needed
 string allocateResultStatus = allocateResult.StatusCode.ToString();

 //completing the task with a specific action
 //TODO: specify the name of one of the available actions that you want to apply when completing the task
 string actionName = "Approve";
 string completeTaskURL = K2ServerURL + worklistTasksEndpointURI + serialNumber + @ "/actions/" + actionName;
 //if you want to update workflow datafields/variables when completing the task,
 //you need to define the datafields and serialize them as a JSON string.
 //Either hardcode the string as in this example, or create your own DataContract class and serialize it
 string datafieldsToUpdateJSON = "{\"dataFields\":{\"TextDatafield\": \"newStringValue\"}}";
 System.Net.Http.StringContent completeTaskHttpContent = new System.Net.Http.StringContent(datafieldsToUpdateJSON, Encoding.UTF8, "application/json");
 var completeResult = httpClient.PostAsync(completeTaskURL, completeTaskHttpContent).Result;
 //do something with the result, if needed
 string completeResultStatus = completeResult.StatusCode.ToString();
}


// Task/worklistitem data contract for the /tasks/{serialNumber} endpoint
[DataContract]
public class K2Task {
 [DataMember(Name = "actions")]
 public Actions Actions {   get;   set;  }

 [DataMember(Name = "activityDataFields")]
 public ActivityDataFields ActivityDataFields {   get;   set;  }

 [DataMember(Name = "activityInstanceDestinationID")]
 public long ActivityInstanceDestinationID {   get;   set;  }

 [DataMember(Name = "activityInstanceID")]
 public long ActivityInstanceID {   get;   set;  }

 [DataMember(Name = "activityName")]
 public string ActivityName {   get;   set;  }

 [DataMember(Name = "eventDescription")]
 public string EventDescription {   get;   set;  }

 [DataMember(Name = "eventName")]
 public string EventName {   get;   set;  }

 [DataMember(Name = "formURL")]
 public string FormURL {   get;   set;  }

 [DataMember(Name = "instruction")]
 public string Instruction {   get;   set;  }

 [DataMember(Name = "itemReferences")]
 public ActivityDataFields ItemReferences {   get;   set;  }

 [DataMember(Name = "itemReferencesString")]
 public string ItemReferencesString {   get;   set;  }

 [DataMember(Name = "originator")]
 public Originator Originator {   get;   set;  }

 [DataMember(Name = "priority")]
 public long Priority {   get;   set;  }

 [DataMember(Name = "serialNumber")]
 public string SerialNumber {   get;   set;  }

 [DataMember(Name = "sleepUntil")]
 public string SleepUntil {   get;   set;  }

 [DataMember(Name = "status")]
 public string Status {   get;   set;  }

 [DataMember(Name = "taskStartDate")]
 public string TaskStartDate {   get;   set;  }

 [DataMember(Name = "viewFlowURL")]
 public string ViewFlowURL {   get;   set;  }

 [DataMember(Name = "workflowCategory")]
 public string WorkflowCategory {   get;   set;  }

 [DataMember(Name = "workflowDisplayName")]
 public string WorkflowDisplayName {   get;   set;  }

 [DataMember(Name = "workflowID")]
 public long WorkflowID {   get;   set;  }

 [DataMember(Name = "workflowInstanceDataFields")]
 public ActivityDataFields WorkflowInstanceDataFields {   get;   set;  }

 [DataMember(Name = "workflowInstanceDataFieldsString")]
 public string WorkflowInstanceDataFieldsString {   get;   set;  }

 [DataMember(Name = "workflowInstanceFolio")]
 public string WorkflowInstanceFolio {   get;   set;  }

 [DataMember(Name = "workflowInstanceID")]
 public long WorkflowInstanceID {   get;   set;  }

 [DataMember(Name = "workflowInstanceXmlFields")]
 public WorkflowInstanceXmlField[] WorkflowInstanceXmlFields {   get;   set;  }

 [DataMember(Name = "workflowName")]
 public string WorkflowName {   get;   set;  }
}


[DataContract]
public class WorkflowInstanceXmlField {
 [DataMember(Name = "name")]
 public string Name {   get;   set;  }

 [DataMember(Name = "value")]
 public string Value {   get;   set;  }
}


[DataContract]
public class ActivityDataFields {}


[DataContract]
public class Actions {
 [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 Originator {
 [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;  }
}