kevin
kevin

Reputation: 14065

SharePoint Security Validation Issue while updating metadata (The security validation for this page is invalid)

I got this error while I call this method from my aspx.cs.

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

           //SPUtility.ValidateFormDigest(); // still not working although I added it later //hit the error here **** if I add
                using (SPSite site = new SPSite(spServerURL))
                {
                    using (SPWeb oWebsite = site.OpenWeb())
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite elevatedSite = new SPSite(site.ID))
                            {
                                using (SPWeb elevatedWeb = elevatedSite.OpenWeb(oWebsite.ID))
                                {
                                    if (elevatedWeb.GetFile(DocumentLibraryName + "/" + folderName + "/" + fileName).Exists)
                                    {
                                        elevatedSite.AllowUnsafeUpdates = true;
                                        #region hitting error
                                        SPFile file = elevatedWeb.GetFile(spDocumentLibraryName + "/" + folderName + "/" + fileName);
                                        file.SetProperty("APPNO", "Test");
                                        file.Update(); //hit the error *****

                                        #endregion
                                        elevatedSite.AllowUnsafeUpdates = false;
                                    }
                                }
                            }
                        });
                    }
                }

I saw some say that if we need to call

SPUtility.ValidateFormDigest(); //hit the error **

method but as soon as I put this line in my codes , I hit the error in this line.

How should I solve it ?

Upvotes: 0

Views: 5164

Answers (2)

kevin
kevin

Reputation: 14065

It's working well after I substitute elevatedSite.AllowUnsafeUpdates with elevatedWeb.AllowUnsafeUpdates !!

Upvotes: 4

Sigar Dave
Sigar Dave

Reputation: 2654

Did you put the control

in your master page , to which this page is referring ?

If no then put it there and may be this error will vanish.

Let me know your outcome.

Upvotes: 0

Related Questions