"Allow Unsafe Updates" in Sharepoint...

Leave a Comment
AllowUnsafeUpdates is set to true when you are trying to update the database as a result of the GET request.
When we have a list and we want to update something, then we need to set AllowUnsafeUpdates = true for the web and after you have done you need to set it back to false.
AllowUnsafeUpdates = false protects from cross site scripting.
So if you need to allow your code to make some updates, you need to set allow unsafe updates to true and then back to false as soon you update.

SPList list= web.Lists.TryGetList["My List Name"];

SPListItemCollection items= list.GetItems();

web.AllowUnsafeUpdates = true;
foreach (SPListItem item in items)
{
     item["ColumnName"] = "Value";
     item.Update();
}
web.AllowUnsafeUpdates = false; 
 If we don' set AllowUnsafeUpdates to true we will get this exception:
 
System.Exception: Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. —> System.Runtime.InteropServices.COMException (0x8102006D): The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
 

Related Post

0 comments:

Post a Comment