Tag Archives: Server Event

K2 Server Event Run As API

In K2 blackpearl workflow, you can specify “Run As” credentials on your Server Events. During workflow execution, the event will be ran using the specified account instead of the K2 Service Account (Note: All Server Events are ran using the service account by default). This feature is available in the K2 Studio and K2 Designer in Visual Studio and it is particularly useful when you need to run critical tasks like create create new AD account, provision Exchange mailbox etc. You do not want the K2 Service Account to have all these critical rights and no Developer should create these critical workflows without the K2 Administrator’s knowledge.

Updating K2 Server Event Run As via K2 Workspace
Updating K2 Server Event Run As via K2 Workspace

Now, the problematic part comes when the Development environment is running on the Development AD whereas the UAT and Production running on the Production AD. It is not possible to embed the credentials in the Server Events now and the only other option is to set it in the K2 Workspace (See article). Setting the credentials in K2 Workspace requires the Administrator to update it on every deployment since it is version specific. Things could get worst when there are multiple events to update in a workflow. To make things simple, we can write a console application that makes use of the workflow management api to update the credentials.

Let’s Code K2 Server Event Run As API

You can find a copy of the working codes at GitHub repository.

The logical steps go like this:

1. Connect to the Workflow Management Server. You need to run using a K2 Administrator account.

2. Get the Process Set for your process. Process Set contains the generic configuration for a Process and is the parent container for all the versions of this process.

3. Find the default Process version. This is required for “Run As” credential setting is version specific.

4. Find the Activity that holds the Server Event which you want to update.

5. Find the required Event.

6a. To set a credential.

6b. To remove the credential and event to service account.


And that’s the end of how to code K2 Server Event Run As API.