Attach EPiServer event handlers on startup using InitializableModule
Explanation of the new InitializableModule concept in EPiServer 6 which is used to implement functionality that should execute when an EPiServer site starts, for example to attach event handlers.
New initialization process in EPiServer 6
In EPiServer 5 we commonly used the PlugInAttribute attribute class to execute code when an EPiServer site starts, for example to attach event handlers.
In EPiServer 6 this can be done by implementing the IInitializableModule interface and decorating the class with the InitializableModule attribute.
An InitializableModule class will be executed on startup, invoking its Initialize method. The order in which these modules are initialized is determined by any dependencies existing between different modules. But EPiServer takes care of all that, as long as you specify which modules (if any) your module depends on.
Create a new InitializableModule class to attach event handlers
Here’s a sample InitializableModule class which attaches an event handler to the CreatedPage event. Note that a dependency has been specified using the ModuleDependency attribute which accepts a single type, or an array of types, which specify which other InitializableModule types must be initialized before the current module. This attribute is optional, but you must set the InitializableModule attribute in addition to implementing the IInitializableModule interface:
public class MyStartupModule : IInitializableModule
private bool _eventsAttached = false;
#region IInitializableModule Members
public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
// Attach event handlers unless they've already been attached
// The initialize method may be executed repeatedly if an exception
// is thrown, but we don't want additional event handlers to attach
// if the exception is thrown later in the call stack
// Attach event handler to when a page has been created
_eventsAttached = true;
public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
// Detach event handlers
DataFactory.Instance.CreatedPage -= CreatedPageHandler;
public void Preload(string parameters)
throw new NotImplementedException("No preload required");
void CreatedPageHandler(object sender, PageEventArgs e)
// Do something with the new page