10

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:

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.

But: The .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.

1 Answer 1

3

Most likely, you're activating the feature from Visual Studio.

Here is a very interesting article about this issue from Waldek Mastykarz:

So, you should try to set "Activate on Default" property for the feature to False.

About SPWebConfigModification class - I know at least 2 weird issues around it:

  1. How SharePoint manages web.config via SPWebConfigModification - SharePoint does not validate the modification XML, and you could stuck there for hours/days, trying to rollback your changes after trivial mistake in your XML.
  2. When you reactivate feature with SPWebConfigModifications, sometimes line <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> dissapears from your web.config :) Magically! So, after this, you will not be able to connect to your site using SharePoint Designer, and also SharePoint webservices will not work.

We are still using SPWebConfigModifications at work (actually, we simply don't have much spare time to fix this). But I'm thinking hardly about replacement for them :)

2
  • a bit of this, a bit of that - as it's a long answer I included it in my question. Good response though.
    – Dennis G
    Commented May 4, 2011 at 14:52
  • did you find a solution how to do SPWebConfigModification for one web application?§
    – Imen Turki
    Commented Nov 23, 2020 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.