A feature requires adding a new connection string. Which should be the way to do this in Helix?
1 Answer
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:
-
-
Note that for connection strings the <configuration> node must be removed. Thanks :)– jgondevCommented Nov 1, 2016 at 8:52
-
Yes - that is because the
ConnectionStrings.config
lives outside the mainweb.config
. I'll update the answer.– Richard Seal ♦Commented Nov 1, 2016 at 12:19 -
So does this Transform just copy the edit in
ConnectionStrings.config.transform
into theConnectionStrings.config
? Why not just directly edit the ConnectionStrings.config in that case? Or better yet don't edit it and just deployConnectionStrings.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? Commented Nov 19, 2019 at 22:22 -
1This 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 ♦Commented Nov 20, 2019 at 5:04