Handle global events

Global events allow you to execute custom code whenever specific actions occur within the system. Events can be raised as a result of both user interaction and the logic of the application itself (default or custom). When you assign a method as a handler for an event, the system automatically executes the code whenever the corresponding action occurs.

For example, when a new page is added to your website, you can use an event handler to load the page’s data, send out the content by email, and use a third-party component to generate a PDF version of the page.

Assign handlers to events

Object and content events

Object events represent the most common actions that occur in the system and are triggered when objects are created, updated or deleted. Content events are triggered during the lifecycle of content items, website channel pages, and headless items.

The Xperience API provides a way to implement object and content event handlers with full support for code using the async/await pattern.

See Handle object events and Handle content events for detailed information.

Other events

Outside of object and content events, register handlers using code in the following format:

<event class>.<event action>.<event type> += <handler method name>

  • Event class – event classes are containers of events related to groups of functionality
  • Event action – represents a specific action that occurs within the system
  • Event type – determines when exactly the event takes place, typically Before or After the action that invokes it. Some actions only have one type: Execute

For example:

C#

BizFormItemEvents.Insert.After += Form_Insert_After;

The event handlers provide parameters derived from EventArgs, which you can use to access data related to the action that occurred. The exact type of the parameter depends on the event.

For information about the available event classes, actions, event types and handler parameters, see: Reference - Global system events

You need to register event handlers at the beginning of the application’s lifecycle (during application startup). See Run code on application startup for more information.

Example

The following steps describe how to register methods as global event handlers:

  1. Open your project in your preferred IDE.

  2. Add custom initialization code that handles the required events.

    • Add the code into a custom Class Library project within your solution. See Integrate custom code for more information.
C#


using CMS;
using CMS.DataEngine;
using CMS.DataProtection;
using CMS.OnlineForms;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomInitializationModule))]

public class CustomInitializationModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomInit"
    public CustomInitializationModule()
        : base("CustomInit")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns custom handlers to events   
        DataProtectionEvents.RevokeConsentAgreement.Execute += RevokeConsentAgreement;
        BizFormItemEvents.Insert.After += Form_Insert_After;
    }

    private void RevokeConsentAgreement(object sender, RevokeConsentAgreementEventArgs e)
    {
        // Add custom actions here
    }

    private void Form_Insert_After(object sender, BizFormItemEventArgs e)
    {
        // Add custom actions here
    }
}

Use events to create integrations

Since events allow you to execute custom code when important actions occur in the system, they can be leveraged to integrate Xperience with external services. For example, integrations can perform operations when content is updated, users submit forms, or errors are written to the event log.

You can implement integrations using either tightly-coupled (shared resources, high dependence between application components) or decoupled architectural patterns (independent components, asynchronous communication). For more information about the latter approach, see Integrate with decoupled systems.