Accessing K2 Environment library via code
K2′s environment library is a great feature allowing you to have a server based config file which can be used by K2 process applications or if you wish other applications as well. K2 workspace allows for the management of this library.
If you want to access this library from your application via code this is what you have to do:
In your .NET app, Add references to the K2 EnvironmentSettings.Client and the SourceCode.Hosting.Client DLLs.
include the following using statements.
then the code looks some thing like this…
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 = 5555;
EnvironmentSettingsManager environmentLibrary = new EnvironmentSettingsManager(false,false);
string output = “”;
if (environmentLibrary.CurrentEnvironment != null)
foreach (EnvironmentField f in
output = output + f.FieldName + “:” + f.Value + Environment.NewLine;
I also found this code chunk using the workflow management API to access the environment library settings.
However this code block will require Admin permissions on K2. I recommend using the Environmentclient API for read access.
If you want to access the environment from within a K2 process though it is pretty easy. The configuration screens allows you to drag and drop a variable from the object browser. If you want to access a field from within a server event you can simply write this line of code:
string env_field_value = K2.StringTable["env field name"];
Since the context “K2″ is passed in to the server event accessing anything from the context such as environment variables becomes very easy.
Access to environment variables can be controlled through K2 workspace by setting permissions. This allows for control over who gets to read/write what template fields.