Saturday, April 21, 2007

ItemAdded event handler vs. ItemAdding event handler

I often get asked this question when should i use the ItemAdded / ItemAdding event handler in MOSS 2007. Well the answer is really simple. If you are adding an item to a list and want to perform a set of actions before the item is saved, including preventing the item from being saved or throw an exception, "ItemAdding" is the event handler to override. If you want to perform post add actions then "ItemAdded" in the event handler to override.

Also note that in your event handler code, you would need to use the "AfterProperties" bag to read the list field values that have changed (as opposed to the "Properties" bag). For example:


private void VowelCheck(SPItemEventProperties properties)
{
string vowelErrorMessage = string.Empty;
// Check whether the Title field exists within the List
if (properties.AfterProperties["Title"] != null)
{
// Assumption that you have a class called VowelListManager.cs which has a function CheckVowelExists()
vowelErrorMessage = VowelListManager.CheckVowelExists(properties.AfterProperties["Title"].ToString());
}

// If Error Message is returned then the current words has failed the vowel test- This code also shows how to cancel a SharePoint event (like stop the item from being added or updated)
if (!vowelErrorMessage.Equals(string.Empty))
{
properties.ErrorMessage = vowelErrorMessage;
properties.Cancel = true;
}
}

4 comments:

Kirti Prajapati said...

Hi,
I have problem in MOSS 2007
My problem is :
I have 5 lists which are having same fields.
Now i want to consolidate all 5 lists in one list (with same fields).
And after consolidating them in one list, i want to make group by view on that consolidated list.
Now when i make changes like insert, update, delete records in the original list (from that 5 list) at that time appropriate change should be done in the consolidated list.
So how can i do this.
provide me code if possible

Anonymous said...

Use a lookup field.

Anonymous said...

Amiable brief and this mail helped me alot in my college assignement. Say thank you you as your information.

Unknown said...

Original ugg boots uk are first category in alter. They are so much appealing louboutin shoes so,