How to set data fields and start an instance of a process

In this example, the console application connects to a K2 server, sets data field values on the process and starts an instance of a process. It requires a reference to the SourceCode.HostClientAPI and the SourceCode.Workflow.Client assemblies, and using statements for both.

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 SetDataFieldsStartProcess() 
						{ 

						// TODO: Replace these placeholder values with values for your environment 
						string _serverName ="blackpearl"; 
						string _user ="K2Student"; 
						string _domain ="DENALLIX"; 
						string _password ="K2pass!"; 
						string _processName ="K2WorkflowProject1\\Process1";
						string _name ="Total Cost"; 
						string _amount ="100"; 
						string _processFolio ="Project 1 Process 1";   

						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 
						Connection connection =new Connection();
  

						try 
						{ 

						//open connection to K2 server 

						connection.Open(_serverName, connectionString.ToString());  

						//create process instance 
						ProcessInstance processInstance = connection.CreateProcessInstance(_processName); 
  

						//populate data fields 
						processInstance.DataFields["Name"].Value = _name; 
						processInstance.DataFields["Amount"].Value = _amount;   

						//set process folio 
						processInstance.Folio = _processFolio + System.DateTime.Today.ToString();   

						//start the process 
						connection.StartProcessInstance(processInstance,false); 
  

						Console.WriteLine("Name: "+ processInstance.DataFields["Name"].Value); 
						Console.WriteLine("Amount: "+ processInstance.DataFields["Amount"].Value); 
						Console.WriteLine("Folio :"+ processInstance.Folio.ToString()); 
						Console.WriteLine("ID: "+ processInstance.ID.ToString()); 
						Console.ReadLine(); 

						}   

						catch(Exception ex) 
						{ 

						Console.WriteLine(ex.Message);
						Console.ReadLine(); 

						}  

						finally 
						{ 

						// close the connection 
						connection.Close(); 

						} 

						} 

						} 

				}

It is not possible to call Update() on the ProcessInstance object from the WorklistItem in the Workflow.Client API without having admin rights for the process.The following example presents a custom impersonation class that can be used when needed to provide those rights.  

public class Impersonation : IDisposable
						{
						private Connection _conn = null;

						private Impersonation(Connection conn)
						{
						_conn = conn;
						}

						#region Impersonate Methods

						public static Impersonation Impersonate(string userName, Connection conn)
						{
						conn.ImpersonateUser(userName);

						return new Impersonation(conn);
						}

						#endregion Impersonate Methods

						#region IDisposable Members

						public void Dispose()
						{
						_conn.RevertUser();
						}

						#endregion
						}
 

						// Calling the impersonation class
Connection conn = OpenK2ClientConnection();
						
						//Impersonate
using( Impersonation.Impersonate(userName, conn )
						{
						//Do whatever work you need to do as the impersonated user
				}