How to retrieve data fields

In this example, the console application connects to a K2 server loops through a worklist. It then writes data fields to the console. XML fields and process instance data could also be accessed while the worklist item is open.

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.

using System; 
						using System.Collections.Generic; 
						using System.Text; 
						using SourceCode.Hosting.Client; 
						using SourceCode.Workflow.Client;   

						namespace K2Samples 
						{ 

						class WorkflowAccessingSample 
						{ 

						public void RetrieveDataFields()
						{ 

						// TODO: Replace these placeholder values with values for your environment 
						string _serverName ="blackpearl"; 
						string _user ="K2Student"; 
						string _domain ="DENALLIX"; 
						string _password ="K2pass!"; 
						string _wfSerialNumber ="4_12"; 

  

						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()); 
  

						try 
						{ 

						// loop through each worklist item 
						foreach(WorklistItem worklistItem in connection.OpenWorklist()) 
						{ 

						// open the worklist item 
						worklistItem.Open();   

						// match with the workflow serial number
						 if(worklistItem !=null&& worklistItem.SerialNumber == _wfSerialNumber) 
						{ 

						// loop through each data field and write the name and value to the console 
						foreach(DataFielddataField in worklistItem.ProcessInstance.DataFields)
						{ 

						Console.WriteLine(dataField.Name +": "+ dataField.Value);
						Console.ReadLine(); 

						} 
						} 

						}                 

						} 

						catch(Exception ex) 
						{ 

						// write error to console 
						Console.WriteLine("Error: "+ ex.Message); 
						Console.ReadLine(); 

						}   

						finally 
						{ 

						// close the connection
						connection.Close(); 

						} 
						} 
						} 
				}