Subscribe to Sitefinity CMS events

You subscribe to an event that is published by a Sitefinity CMS class to write custom logic and have the logic executed when the specific event is raised. When subscribing to an event from the Sitefinity events system, you need to make sure you hook up your event handler at a proper phase during application runtime. You can do that in your Global.asax file or in a custom Subscriber class that is plugged in via the AsssemblyInfo.cs class.

Subscribe to Sitefinity events in Global.asax

Sitefinity CMS is an ASP.NET WebApplication, so you can use the standard Global.asax file approach to work with application and session-level events raised by ASP.NET or HTTP modules. Generally, you do your event subscriptions in the Global.asax file using the system Application_Start method. This method is called when the first resource in an ASP.NET application is requested and can be used to perform startup tasks. Inside the Application_Start method, you can hook up to the events, specific to the Sitefinity application lifecycle, for the purposes of your Following is an example demonstrating how to subscribe to the Sitefinity CMS Bootstrapper.Bootstrapped event that is explained in more details later in this section:

Subscribe to events using PreApplicationStartMethodAttribute class

Sitefinity CMS enables you to subscribe for events using the Application_Start method of the Global.asax file. If you have custom code in the Global.asax file and you do not want to edit the file, you can subscribe to events using the PreApplicationStartMethodAttribute class of the .NET framework. The class provides expanded support for application startup. 

First, you need to create a class that holds the subscription logic - the Subscriber class, for example. Next, you add the PreApplicationStartMethodAttribute class in the AssemblyInfo.cs file. Finally, from the AssemblyInfo.cs file, using the PreApplicationStartMethodAttribute class, you can initialize the Start()method of the Subscriber class. The AssemblyInfo.cs file references an assembly to be linked to during compilation of a dynamic resource.

Create the Subscriber class

The Subscriber class holds the subscription logic for a particular event, for example a Comment event (ICommentEvent).

  1. In Visual Studio, open your Sitefinity CMS project.
  2. In the context menu of the project, click Add » Class...
  3. Name the class Subscriber.cs, for example.
  4. Paste the following code:

In the sample code above, upon application startup, the static method Start() will be called. You use the Bootstrapper class and add an event handler to the Bootstrapped event. Finally, using the Sitefinity CMS EventHub, subscribe for the ICommentEvent and pass the comment event handler that will be fired.

Add the PreApplicationStartMethodAttribute class

To initialize the Start() method of the Subscriber class:

  1. In Visual Studio, open your Sitefinity CMS project.
  2. Expand the Properties folder and open the AssemblyInfo.cs file.
  3. Add the following line:
    [assembly: System.Web.PreApplicationStartMethodAttribute(typeof(SitefinityWebApp.Subscriber), "Start")]
  4. Build the solution.

As a result, the Start() method of the Subscriber class is initialized upon application startup.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

EventHub Events