Work with EventHub events

Each event, both its identity and the associated data, is represented by an interface that inherits the IEventinterface. For instance the IMediaContentDownloadedEventrepresents the event that a media content item (e.g. image) has been downloaded by a user and also defines some properties that provide contextual information about the concrete event – which image, from which library, etc.

Subscribe for an event

To subscribe for a particular event, you call the Subscribe method of the event service and pass the event interface as a generic argument. You can either use the lambda expression syntax to directly pass the executable code, or provide a separate delegate that will be invoked.

Lambda expression

C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Libraries.Web.Events;
using Telerik.Sitefinity.Services;

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

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IMediaContentDownloadedEvent>(evt => LibrariesManager.GetManager().GetDocumentLibrary(evt.LibraryId));
       }
   }
}

Delegate

C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Libraries.Web.Events;
using Telerik.Sitefinity.Services;

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

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IMediaContentDownloadedEvent>(MediaContent_Downloaded_Delegate);
       }
       private void MediaContent_Downloaded_Delegate(IMediaContentDownloadedEvent evt)
       {
           LibrariesManager librariesManager = LibrariesManager.GetManager();
           DocumentLibrary library = librariesManager.GetDocumentLibrary(evt.LibraryId);
       }
   }
}

Unsubscribe from an event

In order to unsubscribe, you need an instance of the delegate you passed when you subscribed. You pass it as an argument to the Unsubscribe method of the event service and again use the event interface as a generic argument.

C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Libraries.Web.Events;
using Telerik.Sitefinity.Services;

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

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IMediaContentDownloadedEvent>(MediaContent_Downloaded_EventHandler);
       }
  
       private void MediaContent_Downloaded_EventHandler(IMediaContentDownloadedEvent evt)
       {
           LibrariesManager librariesManager = LibrariesManager.GetManager();
           DocumentLibrary library = librariesManager.GetDocumentLibrary(evt.LibraryId);
       }
   
       protected void Application_End(object sender, EventArgs e)
       {
           EventHub.Unsubscribe<IMediaContentDownloadedEvent>(MediaContent_Downloaded_EventHandler);
       }
   }
}

"Before" and "after" events

Events exposed by Sitefinity CMS conform to the popular .NET naming convention. Events that are fired before an action are prefixed with “-ing”, while events that fire after an action are sufixed with “-ed”. For example, The “UserCreating” event will fire before a user is created, while the “UserCreated” event will fire after the user has been created.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.