Initiating K2 Process from Custom SharePoint Web Part

K2 Process can be integrated with any ASP.NET applications. It provides client API in the form of assembly that we can include in a Visual Studio ASP.NET Web Application project. The API is provided both in 32 and 64 bit mode. So it is possible to use it from a custom SharePoint web part by referencing its 64 bit assembly.

First, we have to locate the proper K2 client object model assembly to be included in our Web Part Visual Studio project. We have to look it in the server where we install K2 BlackPearl. The default installation directory of K2 BlackPearl should be C:\Program Files (x86)\K2 blackpearl. K2 assemblies are located inside the Bin sub directory if we look for the 32 bit version. But, to be able to use it in custom SharePoint web parts, we should use the 64 bit version that can be obtained from GAC.

To be able to copy an assembly from GAC, we have to change default GAC view in Windows Server 2008 R2. If we browse C:\Windows\assembly from Windows Explorer, what we see is a list of assemblies, but not dll files. We need the dll files. To change Windows Explorer behavior to display assembly dll files, we have to add a DWORD registry entry in HKLM\Software\Microsoft\Fusion registry key. The registry entry name is DisableCacheViewer, the value is 1.

Refresh the content of C:\Windows\Assembly folder, and now we should see a familiar Windows folder structure. Navigate to C:\Windows\Assembly\GAC_64\SourceCode.Workflow.Client\ The assembly we are looking for should be there. Its name is SourceCode.Workflow.Client.dll.

Next, copy the assembly SourceCode.Workflow.Client.dll to development server where we develop our web part with Visual Studio. We can copy it to any folder, for example C:\Assembly, and then put the assembly there. Add a reference to this assembly from within our Visual Studio project. In the code-behind file, add the following using statement.

using SourceCode.Workflow.Client;

Then create a method to initiate K2 Process using its client object model inside our web part’s code-behind. The following code snippet provides a simple example:

private void InitiateWorkflow()
  Connection connection = new Connection();
    string processName = "K2 Process Worfklow";

    ProcessInstance pi = connection.CreateProcessInstance(processName);
    pi.Folio = "Title of Worfklow Item";
    pi.DataFields["Data1"].Value = "Test Data 1"; // Test data field
    pi.DataFields["Data2"].Value = 1; // Test data field

As you can see from the code snippet above, it’s easy to initiate a K2 Process from custom SharePoint web part. We just need to create and open connection. Then we need to create an instance of ProcessInstance class, fill its basic properties, then call StartProcessInstance() method. Note: we use K2 BlackPearl version 4.6.6.

Source: Initiating K2 Process from Custom SharePoint Web Part


Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s