JoelBlogs - Joel Jeffery's Microsoft 365 Blog

Microsoft 365, SharePoint, Teams and Office 365 Architecture, Development, Administration and Training

  • Home
    • Sitemap
  • Articles
    • #SPThingADay
    • SharePoint Online
      • SharePoint Online – Drag and Drop and Large File Uploads
    • SharePoint 2016
    • SharePoint 2013
      • Content Database Changes to the AllUserData Table
    • SharePoint 2010
      • Administration
        • Disable CRL Checking
        • Excel 2010 & PowerPivot
        • Limits & Thresholds
        • PeoplePicker AD Errors
        • Recycle Bin Behaviour
        • Renaming a Server
        • Service Pack 1
        • Unattended Installs
        • Uninstall All SharePoint 2010 Solutions via PowerShell
        • User Alert Management
        • Virtualised SharePoint
        • Visio Stencils for Administrators
      • Development
        • Audience Membership Workflow Activity
        • Base Types, Lists & Content Types
        • BCS & Offline Sync
        • Debugger Skipping Lines
        • Development Laptop Spec
        • Enabling JavaScript IntelliSense
        • Event Receivers & Deployment Jobs
        • FavIcons & SPUrl
        • Google Maps Sandbox Web Part
        • Group By Content Type for List Views
        • Locale Stapler / Master or Default Locale
        • Removing Default Editor Parts
        • Sandbox Embedding Resources
        • Solution Sandbox Introduction
        • SPPersistedObject
        • Restoring Deleted SPSites in SP1
        • SPWebConfigModification 1
        • SPWebConfigModification 2
        • STSADM copyappbincontent vs. Install-SPApplicationContent
        • Workflows for Beginners
        • Workflow InitiationData Seralizer
    • SharePoint 2007
      • Alternate Access Mappings
      • Excel Services
      • Excel Services UDFs & Excel Client 2007
      • Experiences from the Field
      • InfoPath & Forms Server
      • Kerberos & SSRS
      • Records Management
      • Web Application Service
      • WSS vs MOSS
  • Training
    • SharePoint Admin Links
  • Downloads
    • Summary Slides for PowerPoint
    • CodePlex Projects
      • Audience Membership Workflow Activity
      • Google Maps Sandbox Web Part
      • Group By Content Type in List Views
      • Locale Stapler / Master or Default Locale
      • SharePoint Outlook Connector
  • Hire Me!
    • MCP Transcript
    • Résumé/CV

SPWebConfigModification – WebApplication Specific Changes in SharePoint 2010

October 29, 2010 by Joel Jeffery

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);

  12:  

  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();

Links

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

Filed Under: SharePoint Tagged With: Development, SharePoint 2010, SharePoint Architecture

Joel is a full-stack cloud architect who codes. He is a Microsoft Certified SharePoint Online, SharePoint Server and Azure specialist and Microsoft Certified Trainer.
He has over 20 years' experience with SharePoint and the Microsoft .NET Framework.
He's also co-founder of Microsoft Gold Partner JFDI Consulting Ltd. Read More…

Recent Posts

  • Microsoft Flow Tip #1 – Word Templates and Hiding Empty Repeating Sections
  • SharePoint PowerShell Tip #1 – Select-Object and FieldValues
  • Popular Misconceptions – Microsoft Teams relationship with SharePoint
  • Course: Microsoft 365 Certified Teamwork Administrator
  • Audience Targeted Searches in Modern SharePoint Online
MCT 2020-2021
Microsoft Teamwork Administrator Associate
Joel's Acclaim Profile
Joel's Microsoft Profile

Tags

Administration Architecture Certification Cloud Development Information Architecture intranets MCP Microsoft Microsoft Architecture Microsoft Azure migration Mobile Development MOSS MOSS 2007 office365 Office 365 Office 365 PowerShell SaaS SharePoint SharePoint 2010 SharePoint 2010 Training SharePoint 2013 SharePoint Administration SharePoint Administrator SharePoint Architecture SharePoint Designer 2010 SharePoint Developer SharePoint Development SharePoint Online sharepointonline SharePoint Search SharePoint Training SharePoint Videos Silverlight SOA Solution Sandbox SPThingADay TechEd 2007 Training Videos Visual Studio 2010 Windows Phone 7 WSS

Copyright © 2022 Joel Jeffery, SharePoint Architect