---
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 e-mail, 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/k9/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. Choose one of the following options:

- When initializing [custom modules](https://docs.kentico.com/k9/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) – override the **OnInit** method of your module class.
- During the initialization process of the application itself – use the **CMSModuleLoader** partial class in the **App\_Code** folder.

### Example

The following steps describe how to register methods as global event handlers in the App\_Code folder:

> **Note:** **Note**: Registering event handlers using the _CMSModuleLoader_ class only works in the App\_Code folder of the Kentico web project (_CMS_ or _CMSApp_). If you wish to register handlers in a separate assembly (custom project), you need to use the [custom module](https://docs.kentico.com/k9/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) approach.

1. Open your project in Visual Studio.
2. Create a class file in the **App\_Code** folder (or **CMSApp\_AppCode -> Old\_App\_Code** on web application projects).
3. Add a using statement for the **CMS.Base** namespace.
4. Extend the **CMSModuleLoader** [partial class](http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx).
5. Create a new class inside _CMSModuleLoader_ that inherits from **CMSLoaderAttribute**.
6. Add the attribute defined by the internal class before the definition of the _CMSModuleLoader_ partial class.
7. Override the **Init** method inside the attribute class and assign handler methods to the required events.

```csharp

using CMS.Base;
using CMS.DocumentEngine;

[CustomDocumentEvents]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    private class CustomDocumentEventsAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            // 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.
