How to retrieve the worklist of a user and open a worklist item
In this example, the console application connects to a K2 server and retrieves the worklist items for the specified user. It then loops through each worklist item and lists some data about the item along with the actions associated with the item.
The Worklist sample code is written for use with Web based forms. Sample code for Windows based forms will be available in a future release of the Developer's Reference
An exception will occur if you attempt to open a worklist item with an incorrect serial number.
To access the worklist of a user other than the one you used to connect to the server, use the ImpersonateUser method of the Connection class. This is useful when you do not know the password for a user. The account used to connect to the server must have Impersonate rights in order for this method to succeed, and the
If you want to retrieve all worklist items available on the server, see the topic How to access and return information from the Global Worklist
In the following example, provide values for the variables that begin with the underscore (_) character to match your environment and scenario. If pasting this code into a console application, set values for the variables and paste the code into a class called from the Main function.
References to SourceCode.HostClientAPI and SourceCode.Workflow.Client are also required.
If Integration is set to true in the connection string the domain must be specified as part of the user name. eg: DENALLIX\\Anthony instead of just the user name. eg: Anthony
using System; using System.Collections.Generic; using System.Text; using SourceCode.Hosting.Client; using SourceCode.Workflow.Client; namespace K2Samples { class WorkflowAccessingSample { public void RetrieveWorklistOpenItem() { // TODO: Replace these placeholder values with values for your environment string _serverName ="blackpearl"; string _user ="DENALLIX\\Anthony"; string _domain ="DENALLIX"; string _password ="K2pass!"; SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString =new SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder(); connectionString.Authenticate =true; connectionString.Host ="localhost"; connectionString.Integrated =true; connectionString.IsPrimaryLogin =true; connectionString.Port = 5252; connectionString.UserID = _user; connectionString.WindowsDomain = _domain; connectionString.Password = _password; connectionString.SecurityLabelName ="K2";//the default label // open a K2 connection Connection connection =new Connection(); connection.Open(_serverName, connectionString.ToString()); // write current username to console Console.WriteLine("User associated with connection: "+ connection.User.Name); try { // impersonate a different user // the user associated with the connection // must have Impersonate rights on the workflow server connection.ImpersonateUser("Mike"); Console.WriteLine("Current impersonated user: "+ connection.User.Name); // loop through each worklist item foreach(WorklistItem worklistItem in connection.OpenWorklist()) { // open the worklist item connection.OpenWorklistItem(worklistItem.SerialNumber); if(worklistItem !=null) { // retrieve properties of worklist item Console.WriteLine("Process Instance Name: "+ worklistItem.ProcessInstance.Name); Console.WriteLine("Process Destination: "+ worklistItem.ActivityInstanceDestination.Name); Console.WriteLine("Process Folio: "+ worklistItem.ProcessInstance.Folio); Console.WriteLine("Process ViewFlow: "+ worklistItem.ProcessInstance.ViewFlow); // get the workflow actions string workflowActions ="Workflow Actions: "; foreach(SourceCode.Workflow.Client.Action action in worklistItem.Actions) { workflowActions +=Environment.NewLine + action.Name; } // write actions to console Console.WriteLine(workflowActions +Environment.NewLine); Console.ReadLine(); } } // display end of worklist items Console.WriteLine("End of worklist items."); Console.ReadLine(); // revert connection back to original user connection.RevertUser(); } catch(Exceptionex) { // write error to console Console.WriteLine("Error: "+ ex.Message); Console.ReadLine(); } finally { // close the connection connection.Close(); } } } }