3
votes

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();
});
1

1 Answers

0
votes

When the type of web.config modification is EnsureChildNode, the Name property should contain an XPath expression that uniquely identifies the node under the parent node (identified by the Path property) to ensure that duplicates of the node are not added to the file.

In other words, Path + Name must match the XPath expression to the created node.

modification.Path = "configuration/system.webServer/modules";
modification.Name = "add[@name='ErrorRedirectModule2']";
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' />";

I'm not sure about the ' vs. " quotes - it's not consistent even in the MSDN articles I linked below.

More information: