SP 2010: Web Application settings (web.config) modifications in SharePoint 2010 via code

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www. tozit.com | [@zimmergren](http://twitter.com/zimmergren)


I’ve been getting some questions lately about modifications to web.config files and SharePoint 2010 – is it possible to automate so that the administrator don’t have to manually edit those files on each WFE?

Yes, that’s the easy answer. This was possible in SharePoint 2007 as well.

In this article I’ll quickly demonstrate how you (using a Web Application feature) can make modifications to web.config automatically.

Please note that this is just a getting-started point which you’ll have to work deeper into to tweak it to your likings for updates and removals from the web.config file.

Automatic web.config changes using a SharePoint 2010 feature

Basically, if you want to get some type of change into the web.config, it oftentimes has to do with adding custom settings or values that you’ll later reference in your applications.

In this scenario, we’ll add the following key into web.config using a Feature Receiver, so we don’t have to do it manually after/before deploying our solutions:

<addkey= "isAwesome "value= "1 " />

So, in order to achieve this programmatically without actually editing the web.config by hand – you could utilize the class called SPWebConfigModification something like this:

publicoverridevoid FeatureActivated(SPFeatureReceiverProperties properties)


var value = 1; // We want to set the key isAwesome to the value "1"

var webApp = properties.Feature.Parent asSPWebApplication ;

var mySetting = newSPWebConfigModification


Path = "configuration/appSettings",

Name = string.Format("add [@key='isAwesome'] [@value='{0}']", value),

Sequence = 0,

Owner = "Zimmergren.SP2010.WebConfigModifications.Awesomeness",

Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,

Value = string.Format("", value)






By executing this cute snippet of code you will actually add the setting called isAwesome to the web.config of your web application. (This will be persisted to all the WFE’s of course).

Please note: You will need to tweak your code quite a lot to make the sweet SPWebConfigModification class behave the way you want on activation/deactivation of the feature. This should at least give you a starting point :-)


In this short introductory article about web.config modifications I’ve talked a bit about how you can automate the process of altering your web.config files without actually opening them on the file system.
Nothing fancy, but a good starting point for those of you who requested some info on the topic.


About Tobias Zimmergren

Hi, I'm Tobias. I plan, architect and develop software and distributed cloud services. Nice to meet you!