I am trying to update an SPItem from inside a asp.net page but I am getting the error
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
Here is the code I have:
SPItem item = statusList.Items[i];
item["Notes"] = notes + "\r\nThis item was removed from the FTP server";
item["Remove"] = "";
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPContext.Current.Web.Update();
item.Update();
Response.Redirect(Request.RawUrl);
SPContext.Current.Web.AllowUnsafeUpdates = false;
I have also tried
SPContext.Current.Web.Site.WebApplication.FormDigestSettings.Enabled = false;
Which gives me a permissions error.
I have tried impersonating my permissions with
spusertoken systoken = spcontext.current.site.systemaccount.usertoken;
if (systoken == null)
{
spsecurity.runwithelevatedprivileges(
delegate()
{
using (spsite site = new spsite(sharepointstate.siteurl))
{
systoken = site.systemaccount.usertoken;
}
}
);
}
using (spsite site = new spsite(sharepointstate.siteurl, systoken))
{
using (spweb web = site.openweb())
{
item["notes"] = notes + "\r\nthis item was removed from the ftp server";
item["remove"] = "";
spcontext.current.web.allowunsafeupdates = true;
spcontext.current.web.update();
item.update();
response.redirect(request.rawurl);
spcontext.current.web.allowunsafeupdates = false;
}
}
This also gave me the "The security validation for this page is invalid" error. And I've tried running with elevated privileges with this line:
SPSecurity.RunWithElevatedPrivileges(TestSec);
Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest();
Here is the method that it calls:
public void TestSec()
{
int i = 4;
try
{
SPList statusList = SharepointState.Web.Lists.TryGetList("Status List");
SPListItem item = statusList.Items[i];
string name = item["Intake_ID"].ToString().Replace(':', '_');
FTPState state = FtpClass.FTPDelete(name + ".xml");
//delete item from status list if ftp deletion was successful or if the file wasn't found on the ftp server
if (state.StatusDescription.Contains("250") || state.StatusDescription.Contains("550"))
{
string notes = string.Empty;
if (item["Notes"] != null)
notes = item["Notes"].ToString();
item["Notes"] = notes + "\r\nThis item was removed from the FTP server";
item["Remove"] = "";
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPContext.Current.Web.Update();
item.Update();
Response.Redirect(Request.RawUrl);
SPContext.Current.Web.AllowUnsafeUpdates = false;
}
}
catch (Exception ex)
{
ListErrorLogger.WriteError("REMOVE BUTTON EVENT", ex.Message, ex.StackTrace);
}
}
Which gave me the same "The security validation for this page is invalid". Any suggestions?