Skip to main content

Cloud

Team Build 2010: XML Transforms on Team Build 2010 for Non-Web Application Projects

One of the cool new features of Visual Studio 2010 Web Application projects are web.config transforms which allow you to use different settings in your web.config file without needing to remember to change them every time you compile the project. More information can be found here: http://msdn.microsoft.com/en-us/library/dd465326.aspx. In a nutshell, you can provide additional settings and modifications to the web.config that Visual Studio compiles at runtime into a single web.config file.
The obvious question is: what if I want to do this in other projects that aren’t web applications, like SharePoint solutions?

Solution

Fortunately, Johan Leino has discovered a very elegant solution. You can find his solution here: http://johanleino.wordpress.com/2010/12/21/working-with-xml-transformations-in-sharepoint-part-1/. I highly recommend reading and following his example before continuing or you’re likely to get lost with my modifications.
It comes down to creating a .targets file and passing your transforms through the .targets file at compile time. The code he provides will do the actual transformation quite efficiently. However, you run into a problem using his code with Team Build automation and when you just want to package a file.

XML Transforms on Team Build 2010

To get XML transforms to work correctly in all instances, including on Team Build 2010, take the .targets file from Johan’s site and make the following change:
Where he has the line:

 1: <Target Name="CopyTransformedConfigurationFiles"
 2:         DependsOnTargets="$(CopyTransformedConfigurationFilesDependsOn)"
 3:         AfterTargets="Build">

Replace this with the line:

 1: <Target Name="CopyTransformedConfigurationFiles"
 2:         DependsOnTargets="$(CopyTransformedConfigurationFilesDependsOn)"
 3:         BeforeTargets="CleanPackage">

This change moves the step to happen right before the package is created instead of after the package is built and enables the transform to work on Team Build. The purpose of this is twofold:

  1. If you only change your XML files between builds, the files will not be transformed the second time you build.
  2. The way that Team Build handles the target transform file results in the incorrect file being included in the package or the file not being transformed at all.

So there you have it. A little XML and you can mimic the functionality that exists in Web Applications across any other type of XML file.

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Andrew Schwenker

Andrew Schwenker is a Sr. Technical Consultant within Perficient’s Microsoft National Business Unit East's SharePoint practice. Andrew has nearly 2 years of experience in consulting and has participated in projects that have touched nearly every aspect of SharePoint 2010. Andrew earned his Bachelor’s degree in Computer Science as well as Master’s degrees in Computer Science and Information Systems from Indiana University. He’s interested in creating winning solutions to generate business innovation using SharePoint. Prior to starting at Perficient, Andrew completed internships with General Electric, ExactTarget, and Great American Financial Resources. During his studies, he actively participated in Alpha Phi Omega National Service Fraternity and competed in the first annual Cluster Challenge at SC07 in Reno, NV. Andrew was a part of the dynamic PointBridge team that was acquired by Perficient.

More from this Author

Follow Us