Making use of configuration file transformations in SharePoint 2010

The first time I saw the new web.config transform stuff included in VS2010 I was totally amazed and I just kept thinking “I hope I can use that in SharePoint 2010”.
Of course, we couldn´t…well not OOB.

In this blog post I will show you how to bring some love to the SharePoint configuration transformations. In a future post I have “in my head” I´m going to show you how I use settings in SharePoint 2010 using SPG (Patterns and Practices Guidance).
Some background on web.config transformations can be found here: http://msdn.microsoft.com/en-us/library/dd465326.aspx

UPDATE 2011-01-24
I have made an extended series about this topic beginning with this post and continuing with this post.


The goal

The goal is to use different configurations in VS2010 to build different configurations files for the environments we have set up.

Add configuration files

OK, since VS2010 OOB only supports adding new configuration files based on build configurations to WAP (web application projects) we have to add config files manually.

You can see that I have added three app.config files to the root of my project. I´ll use these to add AppSettings to my SharePoint environment. That doesn´t look all that pretty right…let´s fix that.
Edit the project file and find those files:

Modify those nodes so they look this this:

This tells VS to show them underneath the app.config file (just like a code behind):

OK, that´s much better.

Add transformations

The “original” app.config file only works like skeleton, so I´ll just add the keys that I´m going to use in here.

In the app.Debug.config file you can see some fancy new syntax. Not going to tell you about that here, but what I´m doing is telling MSBuild (yeah that´s the magic) to replace the entire appSettings section.

The SharePoint stuff

OK, in SharePoint land we need a feature (and element files) to get something into SharePoint. I use an empty element and in that I add a file called settings.config
This file will be added to the WSP package and it will get its content from that app.config file (so it just needs to exist, no need to include anything in the file). How I´ll use this file I´ll show you in a future post…

The transformation

I played around with a couple of different solutions on how to transform that app.config file (in the resources) but I finally found that using a targets file in my project worked best so let´s look at that:


 <!--Properties-->

 <!--Change this to use another extension like xml, default is config-->
 config

 <!--Change this to use another filename, default is app-->
 app

 <!--Path to where to store the ouput of the transformation-->
 Settings\settings.config

 <!--Import Task TransformXml from Microsoft.Web.Publishing.Tasks.dll-->

 <!--Target ConfigurationTransForm-->

 <!--Cleanup-->

 <!-- Insert transformation targets in the build process -->

 ConfigurationTransForm;
 CopyConfigFile;
 $(BuildDependsOn);

Update 2012-11-02:  the code is apparently “gone”…sorry about that.

A little explanation of the target. First of all there´s a couple of properties that you can change, like what the file names are etc

Then I include the TransformXml target (which does all the magic) and tell that target what files to use for transformation.
The output is just a temp file which I then copy into that Settings\settings.config file and the I delete the temp file…it is actually pretty easy!

Edit the projects file

Edit the projects file to include the new target:


That last line imports my targets file. It is in the project root but you can place it anywhere you like, just make the necessary adjustments to include it.

Here you can see the targets file in my project.

Build the package

So now when you build a package in Debug|Release|Whatever?? the settings.config file will be updated with settings for that configuration before the WSP is built so when the VS environment packages the WSP it will include settings for that configuration…I love it!!

Resources:

http://exceptionalcode.wordpress.com/2010/06/21/visual-studio-app-config-xml-transformation/

http://vishaljoshi.blogspot.com/2010/05/xml-document-transforms-xdt-for-any-xml.html

http://vishaljoshi.blogspot.com/2010/05/applying-xdt-magic-to-appconfig.html

http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

, , , , ,

  1. #1 by Yui Tell on April 11, 2012 - 14:59

    Hello,

    I understand what you are trying to do but what about applying these configs to the web.config ?

    thx

    • #2 by Johan Leino on April 25, 2012 - 20:08

      As stated “The goal is to use different configurations in VS2010 to build different configurations files for the environments we have set up.”. If you choose to apply these changes to a web.config file it’s up to you. Then you have to use (or the best practice is to use) the webconfiguration API…there’s a lot of post flying around on the Internet about that topic including some on my blog also.

  2. #3 by Taka on October 25, 2012 - 23:30

    Is that targets file for real? I’m a bit new to targets files but I’m assuming it’s not complete and is missing a lot of the , tags, right? Do you happen to have the complete version to get your sample to work?

    • #4 by Johan Leino on November 2, 2012 - 23:06

      sorry…it’s gone, WordPress apparently screwed up my code. See any of the future (linked) posts perhaps

  3. #5 by Mathias on March 23, 2015 - 09:53

    The missing code of the targets file can be found on web.archive.org

  1. Working with xml transformations in SharePoint: Part 1 « Johan Leino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: