Wednesday, February 2, 2011

AllowUnsafeUpdates

Problem
While upgrading the item using SPListItem object, getting the below error:

Error
The "WebPart_ObjectModel1" Web Part appears to be causing a problem. Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.

My Code
SPList oList = oWebsite.Lists["Tasks"];
SPListItem oListItem = oList.Items[0];

oListItem["Title"] = "New_Title";
oListItem.Update();


Cause
Microsoft stating that set AllowUnsafeUpdates property of SPWeb is set to true so that the security token validation is not performed. This allows running updates from GET and POST requests and from any type of client application, thus making this the preferred approach in most situations. The following code example shows how to disable the security checks temporarily so that you can rename the current Web's title.

Solution
Set the property of SPWeb - AllowUnsafeUpdates to true

Final Code
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;

SPList oList = oWebsite.Lists["Tasks"];
SPListItem oListItem = oList.Items[0];
oListItem["Title"] = "New_Title";
oListItem.Update();

1 comment:

  1. hi
    i ave the same problem and try web.AllowUnsafeUpdates = true;
    put still give me the same error

    ReplyDelete