Ask a questionAsk a question
 

AnswerApplyWebConfigModifications to specific Site / WebApplication.

  • Monday, November 02, 2009 9:22 PMloonysan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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

  • Wednesday, November 04, 2009 9:19 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

All Replies

  • Tuesday, November 03, 2009 7:10 AMPaul Keijzers KbWorks Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Tuesday, November 03, 2009 7:17 AMHarri K Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Tuesday, November 03, 2009 10:21 AMloonysan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 9:19 AMAaron Han - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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