I’ve previously written a couple of blog posts about Organising your Sitecore Patch Include Files and the subsequent Re-organisation of Include Config Patch files in Sitecore 8. As mentioned in that blog post, in order to ensure that our custom configs are patched in last we can place our own files in
/App_Settings/Include/z.Project. I also raised a Sitecore userVoice request to add additional folder locations for patching.
Recently when providing an answer on Sitecore StackExchange about alternative ways of disabling config files and taking a peek inside the Sitecore.Kernel to post some code for the answer I noticed something…
If you looked at the code snippet in my original post then you would have noticed that the method was
private static 😥
Something had changed…
Oh hello! This makes it ripe for overriding now…
Create a custom class, override this method and add in your own custom directory anywhere. Then update the
sitecore section in the
configSections at the top of web.config to point to this class.
|class ConfigReader : Sitecore.Configuration.ConfigReader|
|protected override void LoadAutoIncludeFiles(XmlNode element)|
|string customFolder = ConfigurationManager.AppSettings["Sitecore.Include.Custom"];|
|ConfigPatcher configPatcher = this.GetConfigPatcher(element);|
|<?xml version="1.0" encoding="utf-8"?>|
|<section name="sitecore" type="MyProject.Custom.ConfigReader, MyProject.Custom" />|
|<add key="Sitecore.Include.Custom" value="/App_Config/Custom_Include" />|
On application start-up the default Sitecore configs will be patched in from
/App_Config/Include and then your own custom folder. Not really a big whoopee but hey, keeps those folders a bit cleaner and one less default Sitecore folder in our solutions.
As an added benefit, a watch is kept on this folder, just like the default config folders, so if anything changes then the application is restarted.
No idea when this was changed, but it’s there in Sitecore 8.1 Update-3
I’ve had an additional thought about this. If you change the ConfigReader to your own implementation, then most likely the offline Sitecore Config Builder will not work correctly since it will not take your custom folder into consideration. The source for the module is available, so it should be easy enough to change though. Sitecore is looking at something for the future, so there is some longer term hope. In the meantime, this was a POC and a PSA, so as always use at your own risk.