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.
Being able to work with Sitefinity events has been around since 5.1 but I've never dug into it until now. After implementing the method to do some customization of a module created with Module Builder, I came away with some knowledge. In this post I'm going to distill what I learned down and share a very short example.
Here's the scenario. I wanted to run some code after an item was published in my "Albums" module. To do that, I added the following code in my Global.asax to subscribe to the IDynamicContentCreatedEvent.
sender, EventArgs e)
sender, ExecutedEventArgs e)
EventHub.Subscribe<IDynamicContentCreatedEvent>(evt => DynamicContentCreatedEventHandler(evt));
var dynamicContentItem = eventInfo.Item;
Albums helper =
I thought, "Great! Now my DynamicContentCreatedEventHandler will be run each time an item is created."
Gotcha #1. Inside my PopulateAlbumInfo method is code to create child items and relate them to the parent Album that was just published. Each time one of those "Track" items was created, my PopulateAlbumInfo method was being called again. I only wanted it to be called when an Album was created so I had to make some code changes.
Type albumType = TypeResolutionService.ResolveType(
var type = dynamicContentItem.GetType();
(type == albumType)
I added a check to make sure that my method was only run when the type of the item I was being notified about was an Album.
"Awesome! Now it's only running when I publish an Album."
Gotcha #2. When an item gets published in Sitefinity, it's a bit more complicated than just persisting the item to the database and moving on with life. There's at least a "Master" and a "Live" version created. (I'm not going to go into detail on them but...just know they're created.) Since I was listening for when any Album item was created, my code was running multiple times; once for "Master" and once for "Live". This wasn't what I wanted so, here's how I fixed it.
(type == albumType && dynamicContentItem.Status == ContentLifecycleStatus.Master)
I added another check to make sure I was only running my custom method when I was notified about the creation of a Master Album.
After building and running the code, everything seemed to be behaving just as I wanted. Through a mishap with leaving Visual Studio in debug mode, I found out this wasn't the case.
Gotcha #3. For some reason, there was a second Master version of the Album being created. I caught wind of it through an exception that was happening in my PopulateAlbumInfo method. Digging through the properties of the item let me see that a lot of the information was simply not there. It was missing things like the title and ID, for starters.
Here's where it got a bit complicated. In my PopulateAlbumInfo method, I was checking out the Master version of the Album to set some additional properties. All I can figure is that since I was listening for the Master version to be created, my code was running before the entire Album item (along with all the versions) had been created. That meant that when I checked it back in, I was inadvertently causing another Album item to be created.
Luckily though, I was able to fix the issue by only running my code after the Live version was created.
(type == albumType && dynamicContentItem.Status == ContentLifecycleStatus.Live)
Success! That's the final version of the code I'm using and it works great.
For more information about the Event System in Sitefinity, head on over to the documentation.
Using Sitefinity events to trigger custom code is a great way to run additional logic when something happens on the site. Also, taking action as soon as an event occurs keeps us from needing to run things as scheduled tasks, which can make things happen more quickly on our sites.
View all posts from Tim Williamson on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
Copyright © 2019 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.