JoelBlogs - Joel Jeffery's Microsoft 365 Blog

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

  • Home
    • Sitemap
  • #SPThingADay
  • Articles
    • 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: Persisting Configuration Data in SharePoint 2010

October 8, 2010 by Joel Jeffery

A common requirement in SharePoint 2010 and SharePoint 2007 developments is having somewhere to save your configuration data. Maybe you need to persist a SQL connection string, maybe you need to save the URL of your accounts system web service or perhaps you want to store settings for your web part that in some way alters the behaviour of your application.

SharePoint offers you many ways of doing this, and each has their merits. In this blog posting – the first in a series – we’ll be looking at persisting configuration data using web.config files.

Persistence Using Web.Config

This is a natural choice for ASP.NET developers. We tend to think in terms of config files when it comes to saving settings for our web-based applications.

Web.config files are also perfectly valid places to store this kind of data in SharePoint, but there are some gotchas.

Gotcha #1: When you make a change to a web.config file, it causes the app domain within the ASP.NET application pool to be recycled for that IIS application root. Any others that share the application pool will also get the rug pulled from under them – you’re effectively doing an IISRESET (albeit only on sites in your app pool), which causes an outage for several seconds, and forces ASP.NET to do a recompile next page load.

Gotcha #2: When you add a new web front end server to your SharePoint farm, it will lose settings that were manually keyed into the web.config on other servers. This is because of the Microsoft SharePoint Foundation Web Application service – the service in charge of keeping IIS in sync across your farm – can only restore web.config changes that it knows about.

Microsoft SharePoint Foundation Web Application Service - Services On This Service in Central Administration

Gotcha #3: In a multi-server farm, any changes you make to web.config on one of your web front end servers must be replicated across all web front end servers.

Gotcha #4: If you have extended your Web Application into other Zones, IIS will create multiple sites to host each Zone. Each IIS site will have its own web.config file for you to keep up to date.

So, how do we avoid these problems? Well, we can’t avoid Gotcha #1. It’s how .NET works. Any web.config changes trigger app pool recycling. But we can avoid Gotcha #2, #3 and #4.

The SPWebConfigModification Class

We can automate additions to, and removals from, the web.config file using the SPWebConfigModification class. It’s a SharePoint utility class that allows us to queue up changes to web.config, and then apply them asynchronously at SharePoint’s leisure, across each IIS site for your Web Application, and each server in your farm.

The most important benefit of doing this is that when I add a new server to my farm or otherwise have to restart the Microsoft SharePoint Foundation Web Application service, I’m guaranteed that all my web.config modifications will be applied to the server.

To use the SPWebConfigModification class, you create an instance of it and set the XPath expression to the target node in web.config you’re editing, then the name of the attribute or node to add, and lastly set the value we want to update.

Then it’s a simple matter of passing your SPWebConfigModification object to the Add()method of the WebConfigModifications collection on your target SPWebService (e.g. SPWebService.ContentService).

To write your changes to the batch, invoke Update() on your SPWebService. but note that nothing will happen until you call ApplyWebConfigModifications().

The code bellow shows the steps to add a new appSetting block to your web.config, adding a key called “mySetting” with the value “http://joelblogs.co.uk”:

   1: SPWebService service = SPWebService.ContentService;

   2:  

   3: SPWebConfigModification myModification = new SPWebConfigModification();

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

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

   6: myModification.Sequence = 0;

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

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

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

  10: service.WebConfigModifications.Add(myModification);

  11:  

  12: /*Call Update and ApplyWebConfigModifications to save changes*/ 

  13: service.Update();

  14: service.ApplyWebConfigModifications();

If you want to read this value later in your code, say for example in a WebPart, then you can use the ConfigurationManager.AppSettings[] collection to access configuration settings in the current web.config.

The code below shows retrieving the value of the “mySetting” key from the appSettings block of your web.config:

   1: string mySetting = ConfigurationManager.AppSettings["mySetting"].ToString();

Advantages

This technique allows you to keep your web.config files in sync, and backed up. Each time SharePoint makes a change to your web.config files, it will create a backup with a time-stamped name in the same folder. It will also ensure that any servers you add, or Zones into which you extend your Web Application, will get the correct web.config entries.

Disadvantages

There are reports that SPWebConfigModification can be flaky. It certainly had issues in SharePoint 2007; use of SPWebModification inside FeatureReceivers is discouraged as sometimes these can go awry. For use elsewhere, make sure the user running the code has administrative permissions to be able to edit the web.config files in the first place.

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…

#SPThingADay

#spthingaday

View
Open
#spthingaday 003 - Information Architecture. Folde #spthingaday 003 - Information Architecture. Folders are evil. Use metadata.  #SharePoint, #Office365..
On a fileshare, all you have is path and filename. That's the only axis by which you can classify your stuff. So you nest folders, within folders, within folders. Then you pack the filename with extra bits of information - like the initials of the last editor, or the version number. 
You don't need to do this in SharePoint. Use metadata. 
Add columns to your libraries. Text columns, number columns, choice columns, managed metadata columns. Each file can have several of these values simultaneously. But a file can only live in one folder at a time. 
Use metadata.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement

#spthingaday 003 - Information Architecture. Folders are evil. Use metadata. #SharePoint, #Office365..
On a fileshare, all you have is path and filename. That's the only axis by which you can classify your stuff. So you nest folders, within folders, within folders. Then you pack the filename with extra bits of information - like the initials of the last editor, or the version number.
You don't need to do this in SharePoint. Use metadata.
Add columns to your libraries. Text columns, number columns, choice columns, managed metadata columns. Each file can have several of these values simultaneously. But a file can only live in one folder at a time.
Use metadata.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement
...

14 0

View
Open
#spthingaday 001 - Governance. Avoid death by file #spthingaday 001 - Governance. Avoid death by fileshare #SharePoint, Teams, Planner and #Office365 projects need executive sponsorship.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement

#spthingaday 001 - Governance. Avoid death by fileshare #SharePoint, Teams, Planner and #Office365 projects need executive sponsorship.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement
...

15 0

View
Open
#spthingaday 060 - SharePoint Search. Changing the #spthingaday 060 - SharePoint Search. Changing the Search Topology.  #SharePoint, #SharePointSearch.. The Search Service in SharePoint on-premise is made up of six components.

Crawl, Content Processing, Index, Analytics, Query and Administration.

Each of these components can be placed on one or more servers in your Farm.

To make any one Search component support high availability, you need to make sure there are at least two servers in your Farm running it.

SharePoint Server 2010 had only 3 types of component, and Microsoft let you manage their placement via Central Admin, in the browser.

From SharePoint 2013 onwards, this was deemed too difficult to express graphically.

For SharePoint 2013 and 2016, topology changes need to be made via PowerShell.

Tomorrow, we'll look at just what PowerShell cmd-lets you need to use.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement

#spthingaday 060 - SharePoint Search. Changing the Search Topology. #SharePoint, #SharePointSearch.. The Search Service in SharePoint on-premise is made up of six components.

Crawl, Content Processing, Index, Analytics, Query and Administration.

Each of these components can be placed on one or more servers in your Farm.

To make any one Search component support high availability, you need to make sure there are at least two servers in your Farm running it.

SharePoint Server 2010 had only 3 types of component, and Microsoft let you manage their placement via Central Admin, in the browser.

From SharePoint 2013 onwards, this was deemed too difficult to express graphically.

For SharePoint 2013 and 2016, topology changes need to be made via PowerShell.

Tomorrow, we'll look at just what PowerShell cmd-lets you need to use.

#microsoft #microsoftoffice #microsoftsharepoint #microsofttraining #officetraining #freetraining #trainingvideos #contentmanagement #enterprisecontentmanagement
...

12 0
Load More...

Recent Posts

  • Course: Microsoft 365 Certified Teamwork Administrator
  • Audience Targeted Searches in Modern SharePoint Online
  • SharePoint Thing a Day – 073 – SharePoint Information Architecture. Content Type Hub Gotchas
  • SharePoint Thing a Day – 072 – SharePoint Information Architecture. The Content Type Hub
  • SharePoint Thing a Day – 071 – SharePoint Information Architecture. Content Type Management Options.
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
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright © 2020 Joel Jeffery, SharePoint Architect