Event fires 3 times when publishing a publishing page

I’ve spent several days working this out and now I finally figured it out. 

The problem:
I created en eventhandler that should be triggered on ItemUpdated on my Pages library. What the eventhandler did isn’t important in this context but I should mention that it didn’t change anything with the involving item (page) so .DisableEventFiring(); wasn’t an issue. The problem was that every time I opened my page, selected edit page, did some changes and clicked publish my event handler was activated 3 times ?!?! 

So I double checked that I had only added one eventhandler (using Eventhandler Explorer) and did al sorts of analyzing and debugging the code, and it let me absolutely nowhere. 

It was only when I stumbled across this blog that I finally figured out what was going on , and from there on out it all became clear. 

The cause:
When working with publishingpages and eventhandlers you should know that your eventhandler ItemUpdated gets fires 3 times when you are in edit mode and publishes a page.
1 time when the page saves (you can  fire this separate this by choosing save)
1 time when the page are checked in (you fire this separate by choosing Check in to share draft)
1 time when the page are published (you can fire this seperate by choosing publish) 

The solution:
So if you only want your eventhandler to do something when the page are actually published you should use the eventhandler ItemCheckedIn instead of ItemAdded and

 use 

if (properties.ListItem.File.Level == SPFileLevel.Published)
{
  // Do something
}
 

This could be one of those cases when you just googles the wrong subjects but I havent been able to find a lot about this subject so i thought I would share it. I hope you’ll find this helpful and that it can save you some headaches.
Advertisements

, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: