Partilhar via


How To : Work with RunWithElevatedPrivileges

Problem:

If the code is not set inside RunWithElevatedPrivileges then you may get following errors

Error:
The security validation for this page is invalid. Click Back in your Web browser,
refresh the page, and try your operation again.
Troubleshoot issues with Windows SharePoint Services.

Resolution:

string siteURL = SPContext.Current.Site.Url;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite objSite = new SPSite(siteURL);
SPWeb objWeb = objSite.OpenWeb();
SPList list = objWeb.Lists["customer"];
SPListItem item = list.GetItemById(1);
objWeb.AllowUnsafeUpdates = true;

item["test"] = System.DateTime.Now.ToString();
item["AlertSent"] = true;
item.Update();

objWeb.AllowUnsafeUpdates = false;
objSite.Close();
objWeb.Close();
});

Note: Please modify the above code with the “List & Field name”(highlighted).