While working on sharepoint object model you may get an error "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."
Following are the ways in which we can get rid of this error.
1.Go to Central Administration->application management-->web application settings->"Web Page Security Validation" and Turn off security validation.
but the above is not valid as it can lead to security voilation and can be dangerous.
2.Setting AllowUnsafeUpdates to True can resolve problem related to Object Model. Setting this property to true opens security risks, potentially open to malicious code.
//Your code goes here
3.Disable the FormDigestSettings for the time of your code execution and should be revert back.
SPSite site = SPContext.Current.Site;
site.WebApplication.FormDigestSettings.Enabled = false; //Your code goes here.. site.WebApplication.FormDigestSettings.Enabled = true; //Or you can use following SPWeb web = site.OpenWeb(); SPWebApplication webApp = web.Site.WebApplication; webApp.FormDigestSettings.Enabled = false; //Your code goes here.. webApp.FormDigestSettings.Enabled = true;
web.AllowUnsafeUpdates = true; //Your code goes here..
web.AllowUnsafeUpdates = false;
Hope this helps.