I have an application page under _layouts, and in it I am trying to do a web.config modification. The page inherits from LayoutsPageBase. The code inside the event handler looks like this:
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (var site = new SPSite(siteId))
{
var webApp = site.WebApplication;
var modification = new SPWebConfigModification();
modification.Name = @"add[@key=""MyKey""]";
modification.Path = "configuration/appSettings";
modification.Value = @"<add key=""MyKey"" value=""MyValue"" />";
modification.Owner = this.Context.User.Identity.Name;
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
webApp.WebConfigModifications.Add(modification);
webApp.WebService.ApplyWebConfigModifications();
webApp.Update();
}
});
However, I always get an exception, even when the user that I am using is a farm administrator! What am I doing wrong?