Bug in Sitecore web.config Include Files

Take care when adding an include file for your Sitecore web.config. These are the files located at your [siteroot]\Website\App_Config\Include, and John West writes about it on his blog here succinctly and articulately, just like every other piece of writing he does on Sitecore.

However, there is a danger if you are trying to insert an XML element without any attributes using an include file! If you do so, the first sibling of your newly inserted node will be removed! The workaround is to add an arbitrary attribute that your application can simply ignore.

For example, look at the following include file that I included at [mysiteroot]\Website\App_Config\Include\Awesome.config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
  <ui>
   <usings>
    <using patch:after="*[text()='System.Collections']">My.Awesome.Namespace</using>
   </usings>
  </ui>
 </sitecore>
</configuration>

The web.config has the following at its /sitecore/ui/usings node

<ui>
 <usings>
  <using>System</using>
  <using>System.Collections</using>
  <using>System.Reflection</using>
  ...
  <using>Sitecore.Xml</using>
 </usings>
</ui>

Then if we visit the expanded web.config which we can view at [yoursite.com]/sitecore/admin/showconfig.aspx, the first node is gone!

<ui>
 <usings>
  <using>System.Collections</using>
  <using>My.Awesome.Namespace</using>
  <using>System.Reflection</using>
  ...
  <using>Sitecore.Xml</using>
 </usings>
</ui>

Talk about annoying! However, if you put in an arbitrary attribute like so

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
  <ui>
   <usings>
    <using patch:after="*[text()='System.Collections']" att="att">My.Awesome.Namespace</using>
   </usings>
  </ui>
 </sitecore>
</configuration>

You won’t get any unintended side effects!

<ui>
 <usings>
  <using>System</using>
  <using>System.Collections</using>
  <using att="att">My.Awesome.Namespace</using>
  <using>System.Reflection</using>
  ...
  <using>Sitecore.Xml</using>
 </usings>
</ui>

Be careful! I’m sure I will have future posts on traps you should be wary of when working with the Sitecore web.config!

comments powered by Disqus