One of the problems I had with list event handlers was event recursion.
I wrote an event handler to handle updating items. I wanted to check some values, and then set a value in a field based on a set of rules. The problem was- when I set the value of the field and updated the item, the ItemUpdated and ItemUpdating events fired up again, and again my code updated the item and so on and so on. I also wanted to avoid having the list item change version number following my code-update. that update was supposed to be part of the version that the user just created by updating. Otherwise, every user update of the list item would bump the version number twice! Similarly, I didn't want the code to show that an administrator changed the item. I wanted it to look as if the user did the change to that field.
So after some research, here is my solution:
item[fieldnamehere] = fieldvaluehere; this.DisableEventFiriing(); item.SystemUpdate(false); this.EnableEventFiring();
This piece of code updates the item without firing the current event handler events, and doing the update using the SystemUpdate(false) method ensures that version number doesn't increase and the update is considered as if it was part of the user's update
Reference : http://www.sharepoint-tips.com/2006/10/preventing-event-handler-recursion.html
I wrote an event handler to handle updating items. I wanted to check some values, and then set a value in a field based on a set of rules. The problem was- when I set the value of the field and updated the item, the ItemUpdated and ItemUpdating events fired up again, and again my code updated the item and so on and so on. I also wanted to avoid having the list item change version number following my code-update. that update was supposed to be part of the version that the user just created by updating. Otherwise, every user update of the list item would bump the version number twice! Similarly, I didn't want the code to show that an administrator changed the item. I wanted it to look as if the user did the change to that field.
So after some research, here is my solution:
item[fieldnamehere] = fieldvaluehere; this.DisableEventFiriing(); item.SystemUpdate(false); this.EnableEventFiring();
This piece of code updates the item without firing the current event handler events, and doing the update using the SystemUpdate(false) method ensures that version number doesn't increase and the update is considered as if it was part of the user's update
Reference : http://www.sharepoint-tips.com/2006/10/preventing-event-handler-recursion.html
Comments
Post a Comment