Monday, March 8, 2010

Save Conflict / Recursive call of event handler / Update item in item updating and adding

Save Conflict / Recursive call of event handler / Update item in item updating and adding


Microsoft.SharePoint.SPException: Save Conflict
Your changes conflict with those made concurrently by another user.
If you want your changes to be applied, click back in your Web browser, refresh the page, and resubmit your changes.

This post is also solution of
Recursive call of event handler and update item in item updating and adding

Once I was suffering from "Save Conflict" error while updating a List item.
This thing happens when to achieve two of my business requirements.

Scenario 1:
In my scenario I have a workflow attached with the list. While adding or updating item from this list, by default workflow is attached to the list. Now as per my requirement. On workflow activated and on task generated again I want to fill one field in the same list. I got "Save conflict" error while updating that list item from workflow.

Scenario 2:
In one of my list in item handler I'm creating a sub site with the Title of the item. What I want to do is that in item added I want to update the list field. But while updating I'm having this "save conflict" error.

So in both the case what I found was that when we are calling
ListItem.Update();
method, list's ItemUpdating event fires and again all the procedure go in to the recursive loop.
To solve this problem try one of these code snippets.

1) (useful while updating List/web from outer side of the scoop of that web or site)
web.AllowUnsafeUpdates = true;
ListItem.SystemUpdate(false);
or
ListItem.Update();
web.AllowUnsafeUpdates = false;
//must set AllowUnsafeUpdates to false if we are set it to true.

2) (Usefull in from updating Item from event handler)
Item[FieldName] = FieldValue;
this.DisableEventFiriing();
item.SystemUpdate(false);
or
item.Update();
this.EnableEventFiring();
//must enable event firing if we are disable it


hopefully this post will help you guys for solving your problem.

Cheers.

0 comments: