Executing Code in Creatio Application and Session Start and Shutdown Events

If you need to execute custom code when the Creatio application starts up or shuts down, or when a user session starts or ends, you can easily do so by creating a class that inherits from AppEventListenerBase and overriding OnAppStart, OnAppEnd, OnSessionStart, or OnSessionEnd.

Let’s take a look at a sample class that executes some code when the Creatio application starts.

using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Web.Common;

namespace Terrasoft.Configuration
{
    public class CustomAppEventListener : AppEventListenerBase
    {
        protected UserConnection GetUserConnection(AppEventContext context)
        {
            var appConnection = context.Application["AppConnection"] as AppConnection;
            return appConnection?.SystemUserConnection;
        }

        public override void OnAppStart(AppEventContext context)
        {
            base.OnAppStart(context);
            var userConnection = GetUserConnection(context);

            // DO SOMETHING ON APPLICATION START HERE
        }
    }
}

Similarly, if you need to respond to a user starting a session, you can use this same approach, however, you’ll get the UserConnection from their session so you can determine the user who started the session.

using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Web.Common;
using Terrasoft.Web.Http.Abstractions;

namespace Terrasoft.Configuration
{
    public class CustomAppEventListener : AppEventListenerBase
    {
        protected UserConnection GetSessionUserConnection()
        {
            return HttpContext.Current.Session["UserConnection"] as UserConnection;
        }

        public override void OnSessionStart(AppEventContext context)
        {
            base.OnSessionStart(context);
            var userConnection = GetSessionUserConnection();
            var user = userConnection.CurrentUser;

            // DO SOMETHING ON SESSION START HERE
        }
    }
}

Along with the above, you can also override:

  • OnAppEnd
  • OnSessionEnd

Note, I’ve used the class name “CustomAppEventListener”, however, you don’t need to name the class this. It can be named whatever you’d like.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *