Dec 18, 2009

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

You get this error message when you are trying to modify something in a sharepoint website be it either creating a list or adding/modifying items to a list in same or different web application from the one you are running your code.
I got this error when I ran this code from a webpart in one webapplication which tried to create a list in another web application in same farm.

Exact error:
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

Resolution:

You need to write two lines of code to resolve this. Basically you need to turn AllowUnsafeUpdates to true before running your code and set it to false after your code executes.

An example code snippet would be:

SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPWeb myWeb = new SPWeb();

myWeb.AllowUnsafeUpdates = true;


//do your code modifications like adding list or list items here

myWeb.AllowUnsafeUpdates = false;

});

Once you do like this, the error should disappear!

1 comment: