I'm struggling with applying a
SPWebConfigModification for just one web application. Microsoft says (SPWebConfigModification Class) in How to: Add and Remove Web.config Settings Programmatically:
To apply a web.config modification to a specific Web application, add the modification to the collection of web.config modifications for the Web application (WebConfigModifications). For example, you can use oWebSite.Site.WebApplication.WebConfigModifications.Add(MyModification) to add a web.config modification to the parent Web application of a specific Web site. You must still call ApplyWebConfigModifications even if you add a web.config modification to a single Web application.
However, when doing so in a WebApplication scoped feature receiver, my web.config changes still get propagated to all WebApplications. I have three WebApplications:
- "WebApp1" -> C:\WebApps\WebApp1
- "WebApp2" -> C:\WebApps\WebApp2
- "WebApp3" -> C:\WebApps\WebApp3
with the respective web.config lying in that folder. When I activate the feature for WebApp1 - all web.configs get my entry. When retracting the feature (and using
.Remove), my entry is only removed from WebApp1's web.config...
Other very good tutorial for web.config changes are the following:
- How To: Modify the web.config file in SharePoint using SPWebConfigModification
- SPWebConfigModification 2010: Change, Caveat, and Code
- SPWebConfigModification’s Top 6 Issues
The problem is that almost everyone wants their web.config changes farmwide - I only want to change the web.config of WebApp1!
Is everybody else doing the powershell way with XML transformations? I would love to use the class Microsoft provided to be used for exactly my scenario...
On a side note: MS also tells you to use additional .config files, please note that these will always be applied farmwide.
Figured it out:
I once used Visual Studio to deploy the feature, hence it got activated for all web applications. I changed that meanwhile though, and I manually have to activate the feature.
.WebConfigModification collection of WebApp2 and WebApp3 still contained the modifcations. And since I queried the content service with
ApplyWebConfigModifications() - all these changes were propaged again to all the other web applications. I actually found out about these cached modifications by querying the database:
SELECT * FROM Objects WHERE Name LIKE '%WebConfigChanges%'.
Second culprit: Visual Studio's deployment doesn't do an iireset - it only recycles Sharepoint the app pools. An iisreset is needed.
Third culprit: When using PowerShell to iterate over the
SPWebApplication.WebConfigModifications collection, PS uses some other method to query the collection than using the object model. Hence PS though the collection was empty, even though it wasn't when queried via the OM.
Crazy Sharepoint world.