---
title: Handle global events
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

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](https://docs.kentico.com/documentation/business-users/content-hub/content-items.md), [website channel pages](https://docs.kentico.com/documentation/business-users/website-content.md), and [headless items](https://docs.kentico.com/documentation/business-users/headless-content.md).

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](https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events/handle-object-events.md) and [Handle content events](https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events/handle-content-events.md) for detailed information.

### Other events

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

**.. +=&#x20;**

- **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:

```csharp
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.

> **Info:** For information about the available event classes, actions, event types and handler parameters, see: [Reference - Global system events](https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events/reference-global-system-events.md)

You need to register event handlers at the beginning of the application's lifecycle (during application startup). See [Run code on application startup](https://docs.kentico.com/documentation/developers-and-admins/customization/run-code-on-application-startup.md) 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](https://docs.kentico.com/documentation/developers-and-admins/customization/run-code-on-application-startup.md) that handles the required events.

   - Add the code into a custom _Class Library_ project within your solution. See [Integrate custom code](https://docs.kentico.com/documentation/developers-and-admins/customization/integrate-custom-code.md) for more information.

```csharp
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](https://docs.kentico.com/documentation/developers-and-admins/integrate-with-decoupled-systems.md).
