---
title: Handling 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.

## Assigning handlers to events

Use code in the following format to register handlers for global events:

**.. +=&#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. Some actions only have one type: _Execute_

For example:

```csharp

DocumentEvents.Update.After += Document_Update_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 full information about the available event classes, actions, event types and handler parameters, see: [Reference - Global system events](https://docs.kentico.com/k12sp/custom-development/handling-global-events/reference-global-system-events.md)

You need to register event handlers at the beginning of the application's life cycle (during initialization):

1. Create a [custom module class](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).
2. Override the module's **OnInit** method and assign handler methods to events.

For basic execution of initialization code, you only need to register a "code-only" module through the API. You do not need to create a new module within the **Modules** application in the Kentico administration interface.

> **Note:** **Handling events on MVC sites**
>
> When utilizing the [MVC development model](https://docs.kentico.com/k12sp/developing-websites/mvc-development-overview.md), you may need to deploy the assembly containing the event registration code and handler methods to the separate MVC application (in addition to the Kentico administration project).
>
> This is required if you are customizing any functionality that occurs on the live site. For example, if you have a custom handler for the _Insert_ event of user objects, the event is raised both when creating a new user in the administration interface (Kentico application) and when a user registers on the live site (MVC application).
>
> For additional information, see [Customizing MVC projects](https://docs.kentico.com/k12sp/custom-development/customizing-mvc-projects.md).

### Example

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

1. Open your Kentico project in Visual Studio (using the **WebSite.sln** or **WebApp.sln** file).
2. Create a [custom module class](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).
   - Add the class into a custom _Class Library_ project within the Kentico solution.
3. Override the module's **OnInit** method and assign handler methods to the required events.

```csharp

using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;

// 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
        DocumentEvents.Insert.After += Document_Insert_After;
        DocumentEvents.InsertLink.Before += Document_InsertLink_Before;
    }

    private void Document_Insert_After(object sender, DocumentEventArgs e)
    {
        // Add custom actions here
    }

    private void Document_InsertLink_Before(object sender, DocumentEventArgs e)
    {
        // Add custom actions here
    }
}

```

See the child pages in this chapter for examples of event handling.
