12

A feature requires adding a new connection string. Which should be the way to do this in Helix?

15

You would need to use an XmlTransform to do this. If you look at the Habitat demo site you can see examples of how to do this.

Creating the transform file

You would add your connection string to a ConnectionStrings.config.transform file - this can live in your project in the App_Config folder.

The file is a standard config transform file. Example:

<?xml version="1.0"?>
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="MyDB" 
        connectionString="value for the deployed Web.config file" 
        xdt:Transform="Insert" />
</connectionStrings>

Merging the transform

If you are using the gulp file from the Habitat example, there is already a task that will apply the transforms in the solution:

04-Apply-Xml-Transform

If you are not using that script, you can do the same thing. It calls MSBuild with an applytransform.targets file.

The targets file contains:

<Project ToolsVersion="14.0" DefaultTargets="ApplyTransform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(MSBuildToolsVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="ApplyTransform">
    <ItemGroup>
      <Transform Include="$(TransformFile)" />

      <ConfigsToTransform Include="$(FileToTransform)" Condition="Exists(@(Transform))">
        <TransformPath>%(Transform.Identity)</TransformPath>
      </ConfigsToTransform>
    </ItemGroup>
    <Message Text="@(ConfigsToTransform)"></Message>
    <Message Text="@(Transform)"></Message>

    <TransformXml Source="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
                  Transform="%(ConfigsToTransform.TransformPath)"
                  Destination="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
                  Condition="Exists(@(Transform))"/>
  </Target>
</Project>

So you can use the MSBuild command line to use that:

msbuild applytransforms.xml /t:ApplyTransforms

More information about how to set this up is here:

  • Beat me to it! ;) – Zachary Kniebel Oct 31 '16 at 16:48
  • Note that for connection strings the <configuration> node must be removed. Thanks :) – JGonDev Nov 1 '16 at 8:52
  • Yes - that is because the ConnectionStrings.config lives outside the main web.config. I'll update the answer. – Richard Seal Nov 1 '16 at 12:19
  • So does this Transform just copy the edit in ConnectionStrings.config.transform into the ConnectionStrings.config? Why not just directly edit the ConnectionStrings.config in that case? Or better yet don't edit it and just deploy ConnectionStrings.config.transform as is to the server, since Sitecore would pick up the config file and include it in its aggregate config (e.g. ShowConfig) I just don't get this answer... why a "merge" step? – Don Cheadle Nov 19 '19 at 22:22
  • 1
    This would be for cases where you need to add new connection strings that were not added on install. E.g for a new publishing target database etc... Also, the connection strings config file is not part of the Sitecore patching include system. It is outside of the <sitecore /> nodes, so you have to deal with it differently. – Richard Seal Nov 20 '19 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.