Accessing K2 Environment library via code

Posted by jEyLaBs on July 14, 2008 in K2 |One Comment

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.

1 Comment

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

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,102 other followers

%d bloggers like this: