I try to add a module into the web.config from within an event-receiver. I use SPWebConfigModification for this. When I Update the webApp (webApp.Update()) the following error is thrown:
The specified node “configuration/system.webserver/modules” was not found in the web.config file.
But that node surely exists in the web config (and also is a quite common node). Any idea why this fails?
SPSite currentSite = GetCurrentSite(properties);
currentSite.AllowUnsafeUpdates = true;
SPWebApplication webApp = currentSite.WebApplication;
SPWebConfigModification modification = new SPWebConfigModification();
modification.Path = @"configuration/system.webServer/modules";
modification.Name = "ErrorRedirectModule";
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
modification.Value = "<add name=\"ErrorRedirectModule2\" type=\"Tools.ErrorHttpModule, Tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6e907fc34eb70f91\" /> ";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
webApp.WebConfigModifications.Add(modification);
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
});
When the type of
web.configmodification isEnsureChildNode, theNameproperty should contain an XPath expression that uniquely identifies the node under the parent node (identified by thePathproperty) to ensure that duplicates of the node are not added to the file.In other words,
Path+Namemust match the XPath expression to the created node.I’m not sure about the
'vs."quotes – it’s not consistent even in the MSDN articles I linked below.More information: