Retrieving the worklist with WorklistCriteria

It is quite possible that a user may have hundreds or even thousands of tasks on their worklist and you want to limit the number of tasks returned, or you may want to locate tasks based on a specific set of search criteria. To limit the number of worklist items returned by the OpenWorklist method, you can instantiate and set up a WorklistCriteria object to build up a combination of filter and/or sorting statements, and then pass this object to the Openworklist() method.

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.
Returning the worklist can be an "expensive" operation and we recommend using WorklistCriteria to limit the number of worklist items returned. You should not retrieve the entire worklist recursively, since this can negatively impact the performance of the K2 server as well

The code snippet below shows how to retrieve a user’s worklist with a simple example of a worklist criteria object where we want to retrieve all workflows where the Folder name is MyFolder, the process Priority is 1 and we want to return the worklist sorted by the Process start date, descending. The code then loops through each worklist item and reads some data about the item.

 This sample code requires references to the assemblies:

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

 //build up criteria for filtering and sorting
 SourceCode.Workflow.Client.WorklistCriteria K2Crit = new WorklistCriteria();
 //example:
 //filter for workflows in MyFolder
 K2Crit.AddFilterField(WCField.ProcessFolder, WCCompare.Equal, "MyFolder");
 //filter for workflows with priority 1
 K2Crit.AddFilterField(WCLogical.And, WCField.ProcessPriority, WCCompare.Equal, 1);
 //sort by workflow start date, descending
 K2Crit.AddSortField(WCField.ProcessStartDate, WCSortOrder.Descending);
 //open the worklist with the given criteria
 SourceCode.Workflow.Client.Worklist K2WList = K2Conn.OpenWorklist(K2Crit);

 //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;
 }
}

Using criteria to return a worklist faster

Using the NoData and Platform parameters in worklist criteria can help to reduce the amoutn of time taken to retrieve a worklist. Note that when you use the NoData flag, no data field values will be returned with the OpenWorklist call.

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

 //use criteria to build a faster worklist retrieval
 SourceCode.Workflow.Client.WorklistCriteria K2Crit = new WorklistCriteria();
 K2Crit.NoData = true; //faster, does not return the data for each item
 K2Crit.Platform = "ASP"; //helps when multiple platform are used
 //Open the Worklist and get the worklistitem count
 SourceCode.Workflow.Client.Worklist K2WList = K2Conn.OpenWorklist(K2Crit);
 //get the number of items in the worklist
 int workItemCount = K2WList.TotalCount;
 //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 Folio = K2WLItem.ProcessInstance.Folio;
 }
}

K2 blackpearl Developers Reference4.7
Video Links Learn Support
No videos found for this article
K2 on YouTube
No Additional links found for this article
No self-learning content for this article
Try some scenarios...
No relevant support links available for this article