sharepoint splistitem.Update fails
I have the following code:
using(SPWeb web = ...
{
....
SPListItem item = GetItemById(id);
item["Status"] = status;
item["Reason"] = reason;
item.Update();
}
And I got an exception "Cannot complete this action"... however, trying to figure it out, I noticed something开发者_JS百科... weird...
I put a breakpoint exactly in the line item.Update()... while debugging I let the exception to happen, and then I went to my Watch window and put there "item.Update()"... the item was updated...
Then.. I started all over again, but, this time, I tried "item.Update()" in the watch window, an exception was thrown.. then I press F10 to execute my code, and for my surprise it successfully updated the item..
I have tried this over and over, and always is the same... so, for some weird reason, the first time it's failing and the second one is good.. does anybody know why?
EDIT:
I've tried this:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
....
item.Update();
});
But, it still fails, now with the exception: "Operation is not valid due to the current state"...
EDIT 2:
this is moss 2007, it is a web application (asp.net) connecting to a sharepoint site, both columns exist..
I've found another thing.. if I put a breakpoint in item.Update(), and I press F10, it works.. the item is updated..and no exception is thrown... but in normal excecution, it always fails..
Try setting:
web.AllowUnsafeUpdates = true;
Also, if you make other updates before this on the SPWeb object or SPListItem object you might need to new them up again.
Check here for some information that might help you: http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/
精彩评论