# Subscribe to Sitefinity CMS events

## Overview

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, and you can use the standard `Global.asax` file 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 CMS application lifecycle, to implement your business objectives.\
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:

```C#
using System;
using Telerik.Sitefinity.Abstractions;

namespace SitefinityWebApp
{
   public class Global_Bootstrapper_Bootstrapped : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           //The Sitefinity application has initialized and is ready, but has not loaded for users yet. 
           //You can execute your code here
       }
   }
}
```

## Subscribe to events using PreApplicationStartMethodAttribute class

Sitefinity CMS enables you to subscribe for events using the `Application_Start` method in 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:
   ```C#
   using System;
   using Telerik.Sitefinity.Abstractions;
   using Telerik.Sitefinity.Data;
   using Telerik.Sitefinity.Services;
   using Telerik.Sitefinity.Services.Comments;

   namespace SitefinityWebApp
   {
       public class Subscriber
       {
           public void Start()
           {
               Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
           }

           private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
           {
              EventHub.Subscribe<ICommentEvent>(evt => CommentsEventHandler(evt));
           }

           public static void CommentsEventHandler(ICommentEvent eventInfo)
           {
               var item = eventInfo.Item;
           }
       }
   }
   ```

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.
