Tuesday, April 24, 2007

How to stop SpListItem.Update() method from recursively calling the ItemUpdated() event handler

When updating a SharePoint 2007 List Item within a custom event handler "ItemUpdated", we need to invoke the Update() method of the ListItem to commit changes to the MOSS DB. However when you invoked the Update() method within the "ItemUpdated" event handler, it triggers a recursive loop and keeps triggering the event again and again. For example:


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

curListItem.Update(); // This line triggers the event to fire again recursively

}

A possible solution is to use the Enable/Disable Event Firing methods available on the SPListItem object. 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 triggers the event to fire again recursively

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

10 comments:

Anonymous said...

I tried using your code but couldn't find the Disable & enable method you have written for the event handler. i am trying to write the code in custom control class . not in an event handler. let me know if there is another way of doing this.

Gavin said...

This is because the DisableEventFiring() method is part of SPEventReceiverBase and additionaly it's a protected method of that class....

Anonymous said...

Does this disabling happen just within the context of this instance of this item being updated, or does it disable event firing for all instances of this/any item being updated?

I hope its only for the first case above.

Anonymous said...

I wish people who put rubbish like this on the internet would stop, or at least correct it.

If it is inside a regular event handler (based on SPItemEventReceiver as Gavin stated) then the lines should read:

this.DisableEventFiring();
&
this.EnableEventFiring();

Keep cowboys away from Sharepoint!

Anonymous said...

Hello all,

I am adding ListItems thru code.
I have written some code for ItemAdded Event to do somework.
But I want to temporarely stop the event Triggering, when i am adding the ListItems thru Code.
Is it Possible?
Thanks In advance.

Anonymous said...

I do not think they are cowboys, i think they are just plan retards!

Anonymous said...

aion chinaaion china gold,
aion cn goldaion chinese gold,
aion gold chinaaion gold chinese,
china aion goldchinese aion gold,
aion china kinaaion chinese kina,
aion kina chinachina aion kina,
aion china buybuy aion china,
aion chinese server

gold
aion cn server

gold
,
aion china server goldchina aion server gold,
chinese aion server

gold
aion chinese server

gold
,
aion cn server kinaaion china server kina,
china aion server kinachinese aion server kinaaion online gold

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
Metal Hanger
Wooden Hanger
plush animal toys
bird toys
buy dog toys
plush toys manufacturer
cartoon toys
bear plush toys
pet bird toys
pet cat toys
best cat toys
wholesale cat toys
Pet Toys
spring suppliers
Lamp Light Fittings
lightning arresters
pin insulators
ceranuc lampholde
fuse cutout

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
meniscus lenses
fused silica
diesel generator
mobile light tower
china diesel engine
diesel engine supplier
china diesel generator
Snow thrower manufacturer
china Diesel water pump
Gasoline water pump
Board Production Line
Plastic Crusher
plastic pipe machinery
Plastic extrusion machinery
China Plastic Crusher
plastic pipe machine
pet strap machine
pp strap machine
Modern bathroom cabinets
stone shower tray
Buy Shower tray
antique bathroom cabinets
shower tray
cheap wash basin
bathroom wash basin
pu sofa leather
pvc leather

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
China Torches
bamboo torches
Pot Packing Candle
Bamboo Torch
total bamboo torch
bamboo garden gazebo
wooden barrels
chinese tea bags
wholesale tea bags
blooming tea
jasmine flower tea
chinese white tea
white tea supplier
white tea exporter
spring Mattress
Spring Clips
Mattress protector
Nonwoven Fabric
Printed fabric
Mattress Fabric