So, I am trying to deploy a Web Application feature that updates the web.config using the WebConfigModifications collection. There is lots of information on doing this including all the issues you run into so I am very confident in the code but no matter what I try the config elements are not added to the web config. This works fine in the development single server environment but does not update in the Farm.
No errors, no log information no event messages .... nothing pointing toward why it will not update:
SPWebConfigModification appSettingModification = new SPWebConfigModification();
appSettingModification.Name = "add[@key=\"Key\"][@value=\"Value\"]";
appSettingModification.Path = "configuration/appSettings";
appSettingModification.Owner = "Owner";
appSettingModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
appSettingModification.Value = __appSettingResource;
if (removeModification)
{
app.WebConfigModifications.Remove(appSettingModification);
}
else
{
app.WebConfigModifications.Add(appSettingModification);
}
app.Update();
app.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
==============================
EDIT
I wanted to add to this based on a suggestion by drax to reflect the ApplyWebModifications method. This method is on the SPWebService that is in the Microsoft.SharePoint.Administration namespace in the Microsoft.SharePoint assembly.
One of the issues with using this method is that all web.config files are updated in the farm even if no modifications are actually being applied. This causes the app pools to be recycled even if you do not intend to update the web config. Reflection shows why this is happening and it is very annoying.
Inside the method is a short foreach loop:
foreach (SPWebApplication application in this.WebApplications)
{
application.ApplyWebConfigModifications();
}
It is looping through each of the web applications and calling an INTERNAL ApplyWebConfigModifications method on the web application objects. If they just made this method public then we would be able to call the method individually without affecting the app pool recycling on web applications that are not affected by our updates. Just sayin'