7SEP 2010

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


Posted by Tobias Zimmergren

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren 

Introduction

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:

 <add key= "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:

 public  override  void  FeatureActivated(SPFeatureReceiverProperties  properties)
 {
     var  value = 1; // We want to set the key isAwesome to the value "1" 

     var  webApp = properties.Feature.Parent as  SPWebApplication ;
     var  mySetting = new  SPWebConfigModification 
     {
         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("<add key='isAwesome' value='{0}' />", 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!

  • Keyur R Patel

    Can I change web.config for a specific web application only, when I activate feature it is deploy on all web applications

    • http://www.zimmergren.net/ Tobias Zimmergren

      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?

      • Keyur R Patel

        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 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 tag. One is old and other is new.

        Thanks in advance

        • http://www.zimmergren.net/ Tobias Zimmergren

          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/reza/2008/01/05/spwebconfigmodifications-top-6-issues/
          - http://www.codeproject.com/Articles/45314/SPWebConfigModification-Tool-and-Explorations
          - http://www.google.com

          Hope it helps.
          Cheers.

          • Alessandro Piccione

            [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 ?

          • http://www.zimmergren.net/ Tobias Zimmergren

            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.