Welcome to K2 Underground Sign In | Join | Help
How to Access Sub-Process Level Context Objects in Exception Handler Block within K2 process

K2 blackpearl provides Exception handling blocks at the multiple levels like Event, Activity and Process level. 

You can access the exception block by clicking on the icon (a yellow triangle with an exclamation mark in it):

This will open up the exception dialog.  You can get to the code by clicking on the 'View Code' window:

 

When the View Code button is clicked the XOML is displayed.  Right click on the XOML canvas and select "View Code".  Once looking at the code, expand the Properties_ExecuteCode method.

The K2 context object exposed here provides details around the error and native workflow entity object access only to ProcessInstance.  In many situations it can be useful to get object level access to entities like ActivityInstance and ServerEvent.  This is handled by casting the K2.ContextObject to the appropriate class.  The appropriate class can be determined by checking the K2.ContextType properties.

Below is an example of where I need to get ServerEvent and ClienEvent context object access for some special exception processing:

 

        private void Properties_ExecuteCode(object sender, EventArgs e)
        {
            K2.AddToErrorLog = K2.Configuration.IsErrorLog;
            K2.AddToServerLog = K2.Configuration.IsServerLog;

            string strType = K2.ContextType.ToString();
            string strEventName = "";
            string strActivityName = "";
            Exception ex = (Exception)K2.ExceptionObject;
            string strErrorMsg = ex.InnerException.Message;
            bool bHandled = false;

            switch (K2.ContextType)
            {
                case (SourceCode.KO.ContextType.ServerEvent):
                    SourceCode.KO.ServerEventContext oServerEvent = (SourceCode.KO.ServerEventContext)K2.ContextObject;
                    strActivityName = oServerEvent.ActivityInstanceDestination.Activity.Name;
                    strEventName = oServerEvent.Event.Name;
                    try
                    {
                        oServerEvent.ActivityInstanceDestination.ActivityInstance.DataFields["ErrorMessage"].Value = strErrorMsg;
                        oServerEvent.ProcessInstance.DataFields["Last Error Message"].Value = "Activity: " + strActivityName + " Error: " + strErrorMsg;
                        oServerEvent.ExpireActivity(strActivityName);
                        bHandled = true;
                    }
                    catch { }

                    break;
                case (SourceCode.KO.ContextType.ClientEvent):
                    SourceCode.KO.ClientEventContext oClientEvent = (SourceCode.KO.ClientEventContext)K2.ContextObject;
                    strActivityName = oClientEvent.ActivityInstanceDestination.Activity.Name;
                    strEventName = oClientEvent.Event.Name;
                    try
                    {
                        oClientEvent.ActivityInstanceDestination.ActivityInstance.DataFields["ErrorMessage"].Value = ex.Message;
                        bHandled = true;
                    }
                    catch { }

                    break;
            }

            // if this wasn't handled explictly then handle the normal K2 way
            if (!bHandled)
            {
                K2.AddToErrorLog = true;
                K2.AddToServerLog = true;
            }

        }

 

Published Friday, March 21, 2008 2:11 AM by Bob

Comments

No Comments

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