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
}

22 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....

soccer man 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...

My friends like to play it and buy aoc gold. If you have money to buy age of conan gold, you will find it is very useful. Earning conan gold is not so hard. Try your best and then you can get it. I buy cheap aoc gold, just because I like it. So simple the aoc money is.

I am so happy to get some aion kina from my friends. They know I need aion online kina, they give me. So I always can get some aion gold from my friends. I buy aion kina with my spare money. It makes me happy that I can still earn some cheap aion kina.

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

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

12Sky Silver Coins said...

Now do you worried about that in the game do not had enough Sword of the New World Vis to play the game, now you can not worried, my friend told me a website, in here you can buy a lot Sword of the New World Gold and only spend a little money, do not hesitate, it was really, in here we had much cheap snw vis, we can sure that you will get the Sword of the New World money, quick to come here to buy vis.

Now do you worried about that in the game do not had enough twelve sky Gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot 12sky gold and only spend a little money, do not hesitate, it was really, in here we had much twelvesky Gold, we can sure that you will get the 12Sky Silver Coins, quick to come here to buy 12 sky gold.

Anonymous said...

Your article is great. Thanks for information. I'ts very useful. You have very nice blog.

------------------------------------------
Free Poker Bonus No Deposit Required - Sign up Promotions.
Free Poker bankrolls - Easy poker money

qingchengzhilian said...

Your blog is very good, it's the best blog I've ever seen, thank you!
By the way, do you like polo shirts, which are very chic, especially the polo t shirts, I love them very much. I also like playing tennis rackets, it can keep healthy, what do you like to do? I'd like to introduce myself as the outlet of polo t shirts women, polo t shirts on sale, and polo t shirts for women. These products are our masterpieces, such as polo shirts on sale, polo shirts men, men's polo shirt, men polo shirt, mens polo shirts, mens polo shirt, besides we also sell cheap polo shirts, discount polo shirts, men's polo shirts, women's polo shirts. We are also the outlet of, cheap tennis racket, discount tennis racket, we recommend prince tennis racquet, head tennis rackets, wilson tennis racket, babolat tennis racquet. And it is our great pleasure that you come to our store online!

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

bike arenal said...

Costa rica tours, shopping online, costa rica fishing, jaco fishing charters, costa rica whale watching, costa rica dolphin watching, costa rica party boat, panama fishing, fishing costa rica los suenos, los suenos fishing charters, costa rica fishing los suenos, los suenos costa rica fishing, costa rica los suenos fishing, fishing costa rica los suenos, los suenos fishing charters, costa rica fishing los suenos, los suenos costa rica fishing charters, costa rica los suenos fishing, Los suenos fishing, Fishing costa rica los suenos, Costa Rica Rafting,
http://www.kingtours.com/.

Roxy said...

Costa Rica Manuel Antonio Rafting, Costa Rica Kayaking, Costa Rica Hiking, Costa Rica Rafting, Costa Rica Bike Tours, Costa Rica Arenal Tours, Costa Rica Guanacaste, Costa Rica Tortuguero,
Costa Rica Gay Travel, Costa Rica Gay Tours,
Costa Rica Los Suenos Vacation Rentals, Costa Rica Los Suenos Fishing, Costa Rica Los Suenos Condos, Los Suenos Costa Rica Tours,
Costa Rica Los Suenos Sportfishing, Costa Rica Los Suenos Resort,
Costa RicaWhale Watching Tours, Costa Rica Dolphing Watching Tours,Costa Rica Dominical, Costa Rica Dominical Tours,

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
Cooler bags wholesale
Drawstring Bags
Cooler Bags
sports backpack
Buy Tote Bags
PVC Coated Hanger
led light manufacturer
LED Tubes
mr16 led bulbs
buy led bulbs
led lighting tube
led ceiling lights
buy led strips
heat transfer press machine
flower plates
photo crystals
Sublimation Products
Sublimation textile
photo crystals
button making machine

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
wholesale mlb jerseys
discount nfl jerseys
cheap soccer jersey
cheap nhl jerseys
discount designer handbags
discount mlb jerseys
cheap mlb jerseys
customized hockey jerseys
globe valve
Wafer Butterfly Valve
cast iron gate valve
Floating ball valve
Y Type Strainers
Flange
Buy Fresh Chestnuts
Chinese Chestnut
Delicious Apple
Red Apple Fruit
Fresh Garlic
pu leather
bag leather
ball leather
PU Synthetic Leather

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!
foldable umbrella
stick umbrella
golf umbrellas
best golf umbrella
opening umbrella
high frequency heat sealing machine
ultrasonic plastic welding machine
vacuum packing machine
discount jerseys
vacuum circuit breaker
porcelain insulator
cable joint
busbar insulator
High voltage products
insulator connector
epoxy insulator
mattress springs
mattress fabric
mattress tape
mattress thread
knitted fabrics
server racks
cheap server rack
wall mount rack
server racks
22u server rack
lady sandals
Children Sandals
Children Slipper
beach slipper
canvas slipper
rubber slipper

King Bayern Munich said...

Hello, friends. The first time to your blog, let me very interested for the class. Write great!
marble suppliers
buy marble
granite suppliers
china stone supplier
china stones
china stone manufacturer
china stone supplier
granite stone manufacturers
granite stone china
Garment Packaging Bags
Plastic flexible packaging
Pet food bags
organ bag
tissue bag
electronics packaging
coffee bags
Underwear bags
cat litter bag
bandpass optical filters
laser line filters
beam splitters
sapphire windows
penta prisms

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