SPWebConfigModification – WebApplication Specific Changes in SharePoint 2010

Following on in my series of posts illustrating the various options for storing your application settings in SharePoint 2010, I had a query this week from one of my students.

How can I make web.config changes that apply to only one SharePoint Web Application?

In my example in part 1 (SPWebConfigModification: Persisting Configuration Data in SharePoint 2010) we learn that the Content Service (SPWebService.ContentService) has a WebConfigModifications collection that allows us to add the modifications we want to make.

It’s also possible to make web.config changes that apply to only the IIS Web Sites belonging to the Zones of a specific Web Application. The SPWebApplication object has its own WebConfigModifications collection.

Gotcha: The act of updating is still initiated by the SPWebService.ContentService object. After setting your WebConfigModifications on the SPWebApplication object, you still need to invoke Update() on ContentService.

   1: SPWebService service = SPWebService.ContentService;

   2: SPWebConfigModification myModification = new SPWebConfigModification();

   3: myModification.Path = "configuration/appSettings";

   4: myModification.Name = "add [@key='mySetting'] [@value='http://joelblogs.co.uk']";

   5: myModification.Sequence = 0;

   6: myModification.Owner = "AD\JOELJ";

   7: myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

   8: myModification.Value = "<add key='mySetting' value='http://joelblogs.co.uk' />";

   9: /*Get reference to the WebApplication we want to administer*/ 

  10: SPWebApplication app = service.WebApplications["SharePoint - 80"];

  11: app.WebConfigModifications.Add(myModification);


  13: /*Call Update on the WebApplication to save changes*/ 

  14: app.Update();

  15: /*Call ApplyWebConfigModifications to cause the Content Service to update the web.config files*/ 

  16: service.ApplyWebConfigModifications();


For more information, have a look at the MSDN article on SPWebConfigModification.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>