4.15.2009

The EventHandler Can't Update Item Data

This story has two part, one is SPItem, another is update failure.
When we update the SPItem property (Field Value) in EventHandler, we know we have to update the data in the event ItemUpdated and not in ItemUpdating. I believe it is easy to find the reference in the www.

We also know we can't utilize the parameter value directly in the ItemUpdated function, we have to initial different instance from new SPSite, SPWeb by the information from patameter, and then we can utilize SPSecurity.RunWithElevatedPrivileges. But in this case, we also have to update property in the difference instance. Why? basically that just my experiences, sometimes the parameter instance will be null when the event handler triggered. Why said sometime? That's also I wonder to understand, in the many many test, I can't figure out why sometime the parameter instance is null. If anyone know the RC, please kindly enlighten me.

But even those, sometimes the instance created by us also be null. Why? I guess that is the latency of the event handler. The following is what I do for it:
SPListItem temp = null;
while (temp == null) {
Thread.Sleep(500);
temp = properties.OpenWeb().Lists[properties.ListId].Folders.GetItemById(properties.ListItemId);
}
Yes, I just sleep the thread..

This really resolve my case for half year, but before couple days, this trick don't work.
After check with MSDN, Here is the Final version.


RETRY2:
int retrycount = 0;
try {
SPListItem temp = null;
while (temp == null) {
Thread.Sleep(500);
temp = properties.OpenWeb().Lists[properties.ListId].Folders.GetItemById(properties.ListItemId);
}
temp[_TSSDocumentFieldName] = url;
temp.Update();

//UPDATE.....
} catch (Exception ex) {
retrycount += 1;
if (retrycount <= 5) {
System.Threading.Thread.Sleep(2500);
goto RETRY2;
} else {
debug.Append("[EXCEPTION][UPDATE PATH 2]" + ex.ToString());
throw ex;
}
} finally {
this.EnableEventFiring();
}
}
}

mm.....actually I am shocked when I saw the sample code in MSDN document.

3 comments:

price per head said...

Wonderful information. I will go to share this to my friends...

Diablo III Gold said...

great pics!!!! very country result, love your current clothing consequently chic and beautiful!!!
RS Money



Guild wars 2 Gold

elo boosting said...

Excellent certainly! Many thanks with this!

LOL Boost



Cheap FUT 14 Coins