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
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.
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…
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!!