ApplyWebConfigModifications to specific Site / WebApplication.
- Hi,
The following code in my FeatureReceiver tries to apply the same webconfig modifications to all the WebApplications present in IIS instead I want it to update only my specific site / webapplication. Any thoughts.
SPWebApplication webApp = site.WebApplication; foreach (SPWebConfigModification modification in this.Modifications) { webApp.WebConfigModifications.Add(modification); } // Save web.config changes. webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); // Serialize the web application state and propagate changes across the farm. webApp.Update();
Thanks, Loonysan | http://mystutter.blogspot.com
Answers
Hi, Loonysan
Instead of using webApp.Farm.Services, would you please try using SPWebService.ContentService to modify the specific Web App’s config file.
Please try changing your code snippet like this:
------------------------------------------------------------------------------------
Uri url = new Uri(SiteUrl);
SPWebApplication webApp = SPWebApplication.Lookup(url);
if (webApp != null)
{
foreach (SPWebConfigModification modification in GetModifications())
{
//Using Id to update specific web app
SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(modification);
}
SPWebService.ContentService.WebApplications[webApp.Id].Update();
SPWebService.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();
}
------------------------------------------------------------------------------------
Hope this can be helpful.
Best Regards,
-Aaron
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 13, 2009 9:12 AM
All Replies
- I think it is a deploy problem code looks fine but if you deploy it could it be that you deploy to all web applications?
kind regards,
Paul Keijzers
Check my website http://www.kbworks.nl or follow me on @KbWorks be sure to Check my KbWorks blog - What do you mean with "tries"?
WebConfigModifications opens every web.config-files but they should only apply the changes to the one where you activated your feature. So even though date modified on other web.configs changes the actual content of those files remains. - ApplyWebConfigModifications - Opens up all webapplication's web.config and tried to apply its webconfigmodifications.
Even if another application's web.config has some issues -the following calls fails with an exception.
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
Is it possible to apply the changes to a specific web.config only and not disturb the other application's web.config
Thanks, Loonysan | http://mystutter.blogspot.com Hi, Loonysan
Instead of using webApp.Farm.Services, would you please try using SPWebService.ContentService to modify the specific Web App’s config file.
Please try changing your code snippet like this:
------------------------------------------------------------------------------------
Uri url = new Uri(SiteUrl);
SPWebApplication webApp = SPWebApplication.Lookup(url);
if (webApp != null)
{
foreach (SPWebConfigModification modification in GetModifications())
{
//Using Id to update specific web app
SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(modification);
}
SPWebService.ContentService.WebApplications[webApp.Id].Update();
SPWebService.ContentService.WebApplications[webApp.Id].WebService.ApplyWebConfigModifications();
}
------------------------------------------------------------------------------------
Hope this can be helpful.
Best Regards,
-Aaron
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 13, 2009 9:12 AM

