---
title: Handling object events
related:
  - https://docs.kentico.com/13/custom-development/handling-global-events.md
  - https://docs.kentico.com/13/custom-development/handling-global-events/reference-global-system-events.md
---

> 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).

Objects include all data structures used within Xperience, such as user accounts, media libraries, search indexes, or setting keys. By handling the events that occur during the life cycle of objects, you can add custom functionality to almost any part of the system. In the API, objects are represented by _Info_ classes.

Use the following classes to access the object events:

- **ObjectEvents** – events triggered for all object types
- **Info.TYPEINFO.Events** – events triggered only for a specific object type, for example: _UserInfo.TYPEINFO.Events_

See the [Global event reference](https://docs.kentico.com/13/custom-development/handling-global-events/reference-global-system-events.md) for information about the available events.

> **Info:** **Note**: Within the context of event handling, objects do NOT include pages in the website content tree. Use the **DocumentEvents** class to assign handlers to events that the system triggers for pages.

## Example

The following example demonstrates how to use event handlers to customize the behavior of a specific type of object.

The sample code extends the functionality of [media libraries](https://docs.kentico.com/13/managing-website-content/working-with-files/media-library-files.md) by creating a handler for the **Insert.After** event of the **MediaLibraryInfo** class. This event occurs whenever a media library is created. The custom logic automatically adds a default sub-folder for every new media library.

1. Open your Xperience solution in Visual Studio.
2. Create a [custom module class](https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).
   - Add the class into a custom assembly (_Class Library_ project) within the solution.
3. Override the module's **OnInit** method and assign a handler method to the **MediaLibraryInfo.TYPEINFO.Events.Insert.After** event.

```csharp

using CMS;
using CMS.DataEngine;
using CMS.MediaLibrary;
using CMS.SiteProvider;

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

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

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

        // Assigns a handler to the Insert.After event for the MediaLibraryInfo class
        // This event occurs after the creation of every new media library
        MediaLibraryInfo.TYPEINFO.Events.Insert.After += MediaLibrary_InsertAfterEventHandler;        
    }

    /// <summary>
    /// Automatically creates a child folder for new media libraries
    /// </summary>
    private void MediaLibrary_InsertAfterEventHandler(object sender, ObjectEventArgs e)
    {
        // Checks that the media library was successfully created
        if (e.Object != null)
        {
            // Gets an info object representing the new media library
            MediaLibraryInfo mediaLibrary = (MediaLibraryInfo)e.Object;

            // Gets the media library's site
            SiteInfo site = SiteInfo.Provider.Get(mediaLibrary.LibrarySiteID);

            // Creates a "Default" sub-folder in the media library
            MediaLibraryInfoProvider.CreateMediaLibraryFolder(site.SiteName, mediaLibrary.LibraryID, "Default");
        }
    }
}

```

To try out the functionality of the custom event handler, create a new media library on any website:

1. Open the **Media libraries** application in the Xperience administration interface.
2. Click **New media library**.
3. Enter a **Display name** and **Folder name** for the new media library.
4. Click **Save**.

The system adds the media library and automatically creates a _Default_ sub-folder.
