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.

using SourceCode.EnvironmentSettings.Client;
using SourceCode.Hosting.Client.BaseAPI;

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.