Working with the Sitefinity Event System

by
Tim Williamson
Jan 09, 2014

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.

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.

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.

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.

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.

1 comment

Nice work Tim. The event system is super powerful indeed! I've used the ILoginCompletedEvent to redirect users to a particular "Portal" based on their Role.
http://www.sitefinity.com/documentation/documentationarticles/ilogincompletedevent