EventHub - capturing global (application-wide, non type-dependent) events using IDataEvent

EventHub - capturing global (application-wide, non type-dependent) events using IDataEvent

Posted on August 01, 2014 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

EventHub is a centralized place in Sitefinity where you can subscribe for data events and execute your custom code when the events are triggered. For example you may subscribe to an event which triggers when a specific content item is deleted and get information about the user who has deleted the item and the date when the item was deleted. You can find a list of the content-specific events exposed by Sitefinity in the following documentation article.

illustration of subscribing for events


However, in some specific scenarios, you might want to have a global event for all content. In these cases you can subscribe to the IDataEvent event which will be thrown on Create, Update and Delete actions for all content. Inside the event handler you can access the following information about the content item:

- Event Action ("Created", "Updated" or "Deleted")
ItemType
ItemId
item ProviderName

Using the above information you will be able to load the corresponding manager and retrieve the actual item which has been created, deleted or updated.

In order to subscribe to the IDataEvent first you need to subscribe to the Bootstrapper_Initialized event in the Application_Start() method in your Global.asax file:

protected void Application_Start(object sender, EventArgs e)
        {
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
        }

After that you need to subscribe to the IDataEvent in the Bootstrapper_Initialized handler as shown below:
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
            if (e.CommandName == "Bootstrapped")
            {
                EventHub.Subscribe<IDataEvent>(Content_Action);           
            }
        }

The next step will be to implement the Content_Action method where you can add your custom logic. Here is a sample code for your convenience demonstrating how you can get the event action, the item type, the item id and the provider name:

private void Content_Action(IDataEvent @event)
        {
            var action = @event.Action;
            var contentType = @event.ItemType;
            var itemId = @event.ItemId;
            var providerName = @event.ProviderName;
            var manager = ManagerBase.GetMappedManager(contentType, providerName);
            var item = manager.GetItemOrDefault(contentType, itemId);
        }

As you can see from the sample code above, after you get the provider name and the item type, you will be able to get the corresponding manager for the content item and then get the actual content item.

To learn more about the Sitefinity event system and the events which are thrown by the Sitefinity modules you can checkout the Sitefinity event system section of our documentation.

Sabrie Nedzhip

Sabrie Nedzhip is a Tech Support Engineer at Telerik. She joined the Sitefinity Support team in December 2013.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation