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)
};
webApp.WebConfigModifications.Add(mySetting);
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
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 :-)
Summary
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.
Enjoy!
Comments are closed
Archived comments
Can I change web.config for a specific web application only, when I activate feature it is deploy on all web applications
Hi Keyur R Patel,
A Web Application Feature is activated per Web Application and not on all Web Applications automatically. Are you deploying through Visual Studio or are you scripting/using Central Administration?
Thanks for quick reply. I have deployed it from Visual studio. Should I try using STSADM command ? Do you think so that will be helpful for me ?
And one another problem is that..
Suppose I have add new child in <httphandlers> tag using code. And deploy from visual studio. It will show my changes in web.config file. But once I change the value of previously add child tag, then deploy, it will show 2 child entries in <httphandlers> tag. One is old and other is new.
Thanks in advance
Hi again,
When deploying from Visual Studio this may very well happen. Try the deployment using PowerShell and target only one Web Application and it should be activated only over the specified Web App.
Also, for the second problem; SPWebConfigModification class is a dragon to wield and it doesn't always work as expected (depending on your expectations, of course). The issue you're experiencing is quite commong, and you should be able to find more information if you search for it.
Some hints for troubleshooting the SPWebConfigModification class:
- http://blogs.devhorizon.com...
- http://www.codeproject.com/...
- www.google.com
Hope it helps.
Cheers.
[quote]"When deploying from Visual Studio this may very well happen."[/quote]
So I can't run "F5" without have this wrong (yes, it is wrong!) behavior ?
Hi Alessandro,
Trial and error mate. Trial and error. I still recommend using PowerShell for these things to get better control, or extending Visual Studio with a custom WebConfig SPI to deal with these specific issues you're seeing.
Cheers,
Tobias.
It seems that even if you put modifications into a SPWeb instance collection and use webApp.Farm.Services.GetValue<spwebservice>().ApplyWebConfigModifications();
It will apply the modifications across the farm. I've yet to find a way to just apply to one web application (it could be one or more sites)
Hi Roman,
You should preferably use a PowerShell script to deploy these modifications. If you're doing it through VS it may not result in the desired output.
Using PowerShell I've never had any problems.
Hi Tobias,
should the Assembly Deploy target be GAC.
When I set the Assembly Deploy Target as web application VS or trows error, when deployed by Powershell SP Admin Farm throws erro
However if the Assembly Deploy is GAC, once deployed and feature scope is web application. the web config is update on all the web application in the farm.
Can you please suggest how can i update only on single targeted web application
Hi Tobias,
I have created the a solution for editing web.config with feature scope as web application, I added and deployed to only one web application.
However the solution gets deployed to all the other web application. for which I had to deactivate them in other web application features.
Is it not possible to deploy to only one web application feature.
Thanks,
Nick.
Hi
I am getting this error when activating feature from UI
"System.Security.SecurityException: Access denied."
Is your issue resolved? As I facing the same error. May I know the solution.
How can we add a new section lets say the <connectionstrings> section?
Refer this blog http://smindreau.wordpress....