As all of us know that event recievers are a great way to hook into various SharePoint events. There can apply to List like FieldAdded, ItemUpdated; to Features such as FeatureActivated and many 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 a 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.
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. The 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 can not be changed" ;
}
}
For a document library, this works just fine. However, it will not work with Lists events. So is it like ItemUpdated or ItemDeleted. NO.
For lists events:
Lists 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
For Document Library:
Lists 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
So if wo go to our original listing of code the code for list should look like this:
public override void ItemUpdating(SPItemEventProperties properties)
{
if (properties.ListItem["column"] != properties.AfterProperties["column"])
{
properties.Cancel = true;
properties.ErrorMessage = "This column can not be changed" ;
}
}
I hope this gives u a better idea about BeforeProperties and AfterProperties.
Tuesday, May 5, 2009
Before and After Properties of SharePoint Event handlers
Before events – Events that fire before an action occurs allow you to perform custom validation, checking, or processing of data that is about to be deleted, modified, or added to a list. These types of events are consistently suffixed with 'ing' to identify them as Before events. Note that the code reacting on these events is executed in a synchronous manner.
After events – Events that fire after a certain action occurs probably are familiar to most developers working with Windows SharePoint Services 2.0. These events are suffixed with 'ed' and the code handling them is executed asynchronously.
After events – Events that fire after a certain action occurs probably are familiar to most developers working with Windows SharePoint Services 2.0. These events are suffixed with 'ed' and the code handling them is executed asynchronously.
Subscribe to:
Comments (Atom)
