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;

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

environmentLibrary.ConnectToServer(connectionString.ConnectionString);

string output = “”;

environmentLibrary.InitializeSettingsManager();

if (environmentLibrary.CurrentEnvironment != null)

{

foreach (EnvironmentField f in

environmentLibrary.CurrentEnvironment.EnvironmentFields)

{

output = output + f.FieldName + “:” + f.Value + Environment.NewLine;

}

}

MessageBox.Show(output);

I also found this code chunk using the workflow management API to access the environment library settings.

http://blogulous.wordpress.com/2008/03/20/k2-accessing-environment-fields-from-code/

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.

One response to “Accessing K2 Environment library via code”

  1. […] Accessing K2 Environment library via code July 2008 […]

Leave a Reply

Discover more from jeylabs

Subscribe now to keep reading and get access to the full archive.

Continue reading