Working with the Sitefinity Event System

Working with the Sitefinity Event System

January 09, 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.

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.


protected void Application_Start(object sender, EventArgs e)
{
    Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
}
 
protected void Bootstrapper_Initialized(object sender, ExecutedEventArgs e)
{
    if (e.CommandName == "Bootstrapped")
    {
        EventHub.Subscribe<IDynamicContentCreatedEvent>(evt => DynamicContentCreatedEventHandler(evt));
    }
}
 
public void DynamicContentCreatedEventHandler(IDynamicContentCreatedEvent eventInfo)
{
var dynamicContentItem = eventInfo.Item;
 
    Albums helper = new Albums();
     
    helper.PopulateAlbumInfo(dynamicContentItem);
}

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.


public void DynamicContentCreatedEventHandler(IDynamicContentCreatedEvent eventInfo)
{
    Type albumType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Albums.Album");
 
    var dynamicContentItem = eventInfo.Item;
    var type = dynamicContentItem.GetType();
 
    if (type == albumType)
    {
        Albums helper = new Albums();
         
        helper.PopulateAlbumInfo(dynamicContentItem);
    }
}

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.


public void DynamicContentCreatedEventHandler(IDynamicContentCreatedEvent eventInfo)
{
    Type albumType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Albums.Album");
 
    var dynamicContentItem = eventInfo.Item;
    var type = dynamicContentItem.GetType();
 
    if (type == albumType && dynamicContentItem.Status == ContentLifecycleStatus.Master)
    {
        Albums helper = new Albums();
         
        helper.PopulateAlbumInfo(dynamicContentItem);
    }
}

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.


public void DynamicContentCreatedEventHandler(IDynamicContentCreatedEvent eventInfo)
{
    Type albumType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Albums.Album");
 
    var dynamicContentItem = eventInfo.Item;
    var type = dynamicContentItem.GetType();
 
    if (type == albumType && dynamicContentItem.Status == ContentLifecycleStatus.Live)
    {
        Albums helper = new Albums();
         
        helper.PopulateAlbumInfo(dynamicContentItem);
    }
}

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.

Tim Williamson

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.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar