Welcome to K2 Underground Sign In | Join | Help
How to pass the K2 context object to a .NET class

Sometimes it is useful to be able to incorporate a .NET class into a K2 process and then simply pass the 'K2' object to it.  You can find the 'K2' object by right clicking on an entity within the Visual Studio K2 process design canvas and selecting 'View Code'.  If you do this on a server event, you should see something like the following:

 namespace ExtenderProject_5209c95b25e34cc9818a87fe0626ef49
{
    public partial class EventItem_1a29b669690b4cabbd32c50172e10a00 : ICodeExtender<hostContext>
 {
        public void Main(Project_2b407f6aa5a8407fa1224a6db17dfa90.EventItemContext_1245195e18b64c50948bf28edce9a5a6 K2)
        {

        }
    }
}

Notice the K2 object that I have put in bold.  This is commonly referred to as the K2 context object since it permits programmatic access to most everything you need at that point in the process definition.

If I now wanted to create a reusuable .NET class that uses this context object I will have problems.  The reason is that you will notice that K2 dynamically generates the object type with a GUID.  Thus this datatype will change from instance to instance.   On the surface that seems to make this reusuable effort impossible.  However there is a way.

1.  Create a simple .NET class in an assembly (that referenced the SourceCode.KO dll).  Notice below that I am using a static object type (not dynamically generated with a GUID) as the input parameter of the method:

namespace ClassLibrary1
{
    public class Class1
    {
        public void SetFolioField(SourceCode.KO.ServerEventContext K2)
        {
            K2.ProcessInstance.Folio = "SET FROM ASSEMBLY VIA CONTEXT OBJECT";
        }  
    }
}

2. I added a reference in my KPRX to this DLL


3. In a Server Event I then added the following code:

        public void Main(Project_2b407f6aa5a8407fa1224a6db17dfa90.EventItemContext_1245195e18b64c50948bf28edce9a5a6 K2)
        {
            ClassLibrary1.Class1 oMyClass = new ClassLibrary1.Class1();     
            oMyClass.SetFolioField(K2.GetServerContext());
        }

Notice in step 3, the K2.GetServerContext() method.  This is the key as it will cast the dynamically generated class into a static object type.

 

Published Thursday, August 21, 2008 3:05 PM by Bob

Comments

# How to pass the K2 context object to a .NET class @ Sunday, October 05, 2008 7:19 AM

Bagaimana caranya parsing k2 context ke assembly kita : 1. Buat class library namespace ClassLibrary1

Agusto Xaverius P Sipahutar

Anonymous comments are disabled
Powered by Community Server (Commercial Edition), by Telligent Systems