As Events of you know, event receivers are a great way to hook into various SharePoint events. These can apply to feature events such Featureactivated, list events such as fieldadded, and stored others. The most Common set of receivers used, however, are part of spitemeventreceiver which Let you wire your code up to a number of events that can occur to items on List or library. When Working with events, you'll quickly find that before (synchronous) and after (Asynchronous) Events exist, and the method suffix such as "ing" (e.g. Itemadding) and "ed" (e.g. itemadded) will tell you whether it gets invoked Before or after the actual change is made. basic stuff. And, As you get deeper, you'll even find that you can extract the before and after State of the change. For example, you can hook into the itemupdating event For a document library and prevent a user from changing a certain column. Code might look like this: Public override void itemupdating (spitemeventproperties properties) { If (properties. beforeproperties ["column"]! = Properties. afterproperties ["column"]) { Properties. Cancel = true; Properties. errormessage = "This column cannot be changed "; } } For A document library, this works just fine. However, you shoshould know that Beforeproperties hash table is not populated for items on a list. As is Worded in the SDK:" Documents,BeforeAndAfterProperties Are guaranteed for post events, such as itemupdated,BeforeProperties are not available Post events on list items" When They say "not available for post events on list items", do they mean after Events (like itemupdated, itemdeleted, etc )? The wording is curious here, so I thought I 'd take some time to test each combination of common events such As add, update and delete. These were done into ss a custom list and then Document Library. Each test involved adding a new item, editing the item and Then deleting the item. Here are the results for a list:
List |
Beforeproperties |
Afterproperties |
Properties. listitem |
Itemadding |
No Value |
New Value |
Null |
Itemadded |
No Value |
New Value |
New Value |
Itemupdating |
No Value |
Changed Value |
Original Value |
Itemupdated |
No Value |
Changed Value |
Changed Value |
Itemdeleting |
No Value |
No Value |
Original Value |
Itemdeleted |
No Value |
No Value |
Null |
No Value means that column value in the hash table was not available. New value means that the correct value for the column was available. Changed value means that the correct updated value was available. Original value means that the correct original value was available. Here Is the same test against a Document Library:
Library |
Beforeproperties |
Afterproperties |
Properties. listitem |
Itemadding |
No Value |
No Value |
Null |
Itemadded |
No Value |
No Value |
No Value |
Itemupdating |
Original Value |
Changed Value |
Original Value |
Itemupdated |
Original Value |
Changed Value |
Changed Value |
Itemdeleting |
No Value |
No Value |
Original Value |
Itemdeleted |
No Value |
No Value |
Null |
Properties. listitem Refers the current value for the list item at that point in the event. Null means that the item is not available. My analysis yields the following Results:
- Not
Surprisingly, we get null values for itemadding (before item is added) And itemdeleted (after item is deleted). This was proven by ishai Sagi some Time ago.
- As correctly
Specified ented In the SDK, item events for lists do not expose Beforeproperties.
- Itemadding and
Itemadded correctly report the value in the afterproperties for an list Item, but not a library item. This is curious.
- We have no
Visibility on the previous states during the itemdeleted event. Once It's deleted, it's clearly gone.
So, If we go back to our original problem listed above. How can we prevent a user From changing a certain column for an item in a list event? From the list Table, you can see if we hook into the itemupdating event, we can compare Current item's value (properties. listitem) to the afterproperties value. Code wocould look like this: If (properties. listitem ["column"]! = Properties. afterproperties ["column"]) { Properties. Cancel = true; Properties. errormessage = "This column cannot be changed "; }
I Hope this post gives you a better idea of how before and after events work For both lists and libraries. Your comments and feedback are always welcomed. |