Welcome to MSDN Blogs Sign in | Join | Help

"The security validation for this page is invalid" error when updating objects through SharePoint object model

This error is often encountered when SharePoint OM is used to update site/web/list objects from within a web context.  Some thing so basic as the code below could fail:

using (SPSite site = new SPSite("http://moss"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["List1"];
        list.Title = "List2";
        list.Update();
        web.Update();
    }
}

Setting the "AllowUnsafeUpdates" properties of both the SPSite and SPWeb objects to "true", mostly will resolve this issue out if you are using a code similar to above within a webpart or an ASP.NET web application.  When you are running from the context of a separate web application make sure the application pool identity running your ASP.NET web application is the same as the one that's running SharePoint's site.  Below is a code that fixes the "security validation for this page is invalid" error.

using (SPSite site = new SPSite("http://moss"))
{
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.OpenWeb())
    {
        web.AllowUnsafeUpdates = true;
        SPList list = web.Lists["List1"];
        list.Title = "List2";
        list.Update();
        web.Update();
    }
}
Thought we might actually prevent some support calls by posting this :)
Published Saturday, June 21, 2008 2:51 PM by sridhara

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# .. of ones and zeros .. » Blog Archive » "The security validation for this page is invalid"

Tuesday, February 17, 2009 6:54 AM by anonymou

# re: "The security validation for this page is invalid" error when updating objects through SharePoint object model

How can i add this code in masterpage

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker