Is this really the version you are looking for? Kentico Xperience 12 Service Pack Documentation is no longer updated. Use the switcher in the header to choose your product version.
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.
Use code in the following format to register handlers for global events:
<event class>.<event action>.<event type> += <handler method name>
For example:
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.
For full 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 life cycle (during initialization):
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.
Handling events on MVC sites
When utilizing the MVC development model, 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.
The following steps describe how to register methods as global event handlers:
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.