Tuesday, April 24, 2007

Updating SPListItem fields in the ItemAdded event or ItemUpdated event does not actually update the ListItem being updated

One common problems which lot of MOSS developers working on custom event handlers face is that their custom events like "ItemAdded" or "ItemUpdated" etc has code updating the various fields in the ListItem but after the "Update()" method is invoked, the new values are not updated in the ListItem. For example:

public override void ItemUpdated(SPItemEventProperties properties)
{
SPListItem curListItem = properties.ListItem;
curListItem["Title"] = "Test";

curListItem.DisableEventFiring(); // Disables event firing temporarily before performing update

curListItem.Update(); // This line should update the ListItem but it doesnt

curListItem.EnableEventFiring(); // Enables event firing once update is complete
}

For the update to go through, its mandatory to set "AllowUnsafeUpdates" property of the Web object with the SPListItem class to true. So the correct version of the above code is as follows:

public override void ItemUpdated(SPItemEventProperties properties)
{
SPListItem curListItem = properties.ListItem;

curListItem.Web.AllowUnsafeUpdates = true; // This property should be set to true for the update to go through

curListItem["Title"] = "Test";

curListItem.DisableEventFiring(); // Disables event firing temporarily before performing update

curListItem.Update(); // This line should update the ListItem but it doesnt

curListItem.EnableEventFiring(); // Enables event firing once update is complete
}

4 comments:

luciusbv said...

As far as I noticed there is no DisableEventFiring() method in SPListItem class.

lijialefw said...

Why was there no follow on bankruptcy then? The bailout of AIG FP went to (wow power leveling) hedge funds that bound credit swaps on Lehman failing or others betting on rating (wow power leveling) declines. AIG has drained over 100 billion from the government. Which had to go to (wow power leveling) those who bet on failures and downgrades. Many of whom (power leveling)were hedge funds. I-banks that had offsetting swaps needed the money from the AIG bailout or they would have been caught. Its an (wow powerleveling) insiders game and it takes just a little bit too much time for most people to think (wow gold) through where the AIG 100 billion bailout money went to, hedge funds and players, many of whom hire from the top ranks of DOJ, Fed, Treasury, CAOBO

qishaya said...

Original christian shoes are first category in alter. They are so much appealing louboutin shoes so, ample of women can not pause to get their hands on them. cheap christian louboutin However, these shoes are not economical. christian louboutin So adequate of women cannot present these shoes. christian louboutin heels Christian Louboutin Replica is presenting the textbook thing for these people. christian louboutin sandals It is present masses of fashionable shoes in an economical rate. christian pumps You know that unique shoes are posh to buy. christian sandals Nevertheless do not think that exchange a shoe with christian louboutin pumps magnificent create of top trait is hard. Now a time model are presented. louboutin pumps But if you hardship attribute cargo look discount christian louboutin for these replicas in a trustworthy position & set an order for your beloved shoes christian louboutin sale

wenwen said...

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