Wednesday, January 11, 2012

How to handle version conflict during Update operation

You can use Microsoft.Office.Server.Utilities.EventReceiverUtility class and IsVersionConflictException method. You can find it useful when you use SPWeb.AllProperties for some application counter for example.

SPWeb web = ...;
bool retry = true;
int count = 0;
while (retry)
{
try
{
int lastId = 0
if (web.AllProperties.ContainsKey("LastId"))
lastId = (int)web.GetProperty("LastId");
else
web.AddProperty("LastId", lastId);

lastId++;
web.SetProperty("LastId", lastId);
retry = false;
}
catch (Exception ex)
{
if (!EventReceiverUtility.IsVersionConflictException(ex))
throw;
if (count > 2)
throw;
}
count++;
}

No comments:

Post a Comment