Pages

11 September, 2012

Dismistifying AfterProperties & BeforeProperties

Hi folks, just logged for a fast writing. Everytime I need to code an event receiver I always face the problem of what to use in order to get the Changed Values and Original Values of the event Item.

Guess we all know the:

  • SPItemEventProperties.AfterProperties
  • SPItemEventProperties.BeforeProperties
  • SPItemEventProperties.ListItem
The pain is to understand what values this properties contain in each of the events of a List or Library.
Did some debugging and found nothing new, yet with a small trick in Library events.

Check it out ...

1. Custom list 


AfterProperties
BeforeProperties
ListItem
ItemAdding
Changed value
Nothing
Null
ItemAdded
Changed value
Nothing
Changed value
ItemUpdating
Changed value
Nothing
Original value
ItemUpdated
Changed value
Nothing
Changed value
ItemDeleting
Nothing
Nothing
Item
ItemDeleted
Nothing
Nothing
Null

2. Library


AfterProperties
BeforeProperties
ListItem
ItemAdding
Nothing
Nothing
Nothing
ItemAdded
Nothing
Nothing
Changed value
ItemUpdating
Changed value
Nothing/Original value
Original value
ItemUpdated
Changed value
Nothing/Original value
Changed value
ItemDeleting
Nothing
Nothing
Item
ItemDeleted
Nothing
Nothing
Nothing

Nothing new right? Well, before writing this article I did a web search and found a board much like this one.
Yet see the change in the BeforeProperties for the Library events.

Why two values for the ItemUpdated and ItemUpdating? Easy enough.

When you add a new document to a document library the events fired are in this order: ItemAdding > ItemAdded. Until here nothing new.

Well right after you add a new document the EditForm pops up in order to allow you to change any properties for the document.

The events that are fired for this Form after you press Save are: ItemUpdating > ItemUpdated.
Yet, at this moment (the moment right after you add a new document), the BeforeProperties has no usefull values like it should. In fact, it has nothing. So, not only the Update events are fired, when your are adding a new document (because infact the document is already saved), as the BeforeProperties work diferently from the standard Update action.

Hope it helped and saves you some time.

If you want to keep in touch, feel free to Subscribe to How I did it - Sharepoint Foundation 2010