Retrieving the worklist

A common requirement is to create a customized K2 worklist or to retrieve all tasks for a user. The OpenWorklist method is the most-commonly used approach to retrieve tasks for a particular user. The OpenWorklist method returns a Worklist object, which contains a collection of WorklistItem.

The code snippet below shows how to connect to a K2 server and retrieve the worklist items for the connected user. It then loops through each worklist item and reads some data about the item.

 This sample code requires references to the assemblies:

  • SourceCode.HostClientAPI
  • SourceCode.Workflow.Client

using (SourceCode.Workflow.Client.Connection K2Conn = new Connection()) {
 //open a simple connection for simplicity
 K2Conn.Open("localhost");

 //retrieve the entire worklist for the connected user
 SourceCode.Workflow.Client.Worklist K2WList = K2Conn.OpenWorklist();

 //iterate over the worklist items in the worklist
 foreach(SourceCode.Workflow.Client.WorklistItem K2WLItem in K2WList) {
  //do something with the worklist item
  //you can query properties/objects contained in the worklist item object
  string serialNumber = K2WLItem.SerialNumber;
  string status = K2WLItem.Status.ToString();
  string Folio = K2WLItem.ProcessInstance.Folio;
 }
}

Retrieving the user's entire worklist as shown in this code sample is only pragmatic in scenarios where users will ever have relatively small worklists (e.g. 50 worklist items or less) at any given time. In a higher-load environment, you should use WorklistCriteria to filter the number of worklist items returned by the OpenWorklist call so as not to overload the K2 server. See the topic
If you want to retrieve all worklist items available on the server as opposed to only worklist items for the current user, see the topic Working with the management (global) Worklist.