Thursday, August 18, 2011

Security validation exception when updating list items

Recently, I faced an exception when trying to update an existing list item programatically. When trying to update the list item i.e., when trying to do SPListItem.Update(), I was getting the below exception message
           
              "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."

After some time, I found that I need to set the "AllowUnsafeUpdates" property of the parent web of the list item to true before doing an update.

               targetWeb.AllowUnsafeUpdates = true;
               listItem["Title"] = newTitle;
               listItem.Update();
               targetWeb.AllowUnsafeUpdates = false;

This should work if the current user has proper privileges, otherwise the above code needs to be ran under an impersonated context.

Probably, the same will happen when creating, deleting a list item too.  Happy coding ...


Senthil

No comments:

Post a Comment