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.
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 is a Tech Support Engineer at Telerik. She joined the Sitefinity Support team in December 2013.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.