Prior to Sitecore 9 config files and server roles could easily be the fuel of nightmares. Want to add you own search configuration? ZZZ folder it is, want to update some cache settings? That’s right. ZZZ folder.
However, all that is in the past as of Sitecore 9, Sitecore introduces Layers! Gone are the era of ZZZ folders.
Well in Sitecore 9 you’ll find a file under App_Config named Layers.config. This is where the magic happens. By default, Sitecore is shipped with four predefined layers:
You can read more about Sitecore layers.
Changes we as developers make should be placed in either the Custom or the Environment layers.
Now you might be thinking great, but how do I control the order of which my config files are patched? After all that’s what the ZZZ thing was all about. I know, and here is how!
The layers have a default loading order which is as follows:
When Sitecore processes a specific layer, it will go through all the subfolders recursively and load the files in these in alphabetical order. Any file placed in the root of a folder is merged before the files in the subfolders.
As explained config files under App_Config/Custom would load as follows:
App_Config/Custom/MyConfig.config
App_Config/Custom/Folder1/File1.config
App_Config/Custom/Folder1/File2.config
App_Config/Custom/Folder2/File1.config
However, what if we wanted the contents of Folder2/File1.config to be patched in prior to the contents of Folder1? No longer do we need to create weird ZZZ folders or name files ZFile1.config or whatever we might have been doing, now we can simply alter the order of which the files patch in a single layer, directly in the App_Config/layers.config file.
We do this by add a loadOrder node in the layers.config file under the specific layer which we wish to work with.
In our example to load the contents of the Folder2 in our Custom layer prior to the contents of Folder1 we would simply add the load Order as follows:
This tells Sitecore that prior to any other folder, we want this folder to be loaded, what is neat about this is that we can do this on both entire folders or specific files. Say we have a file that simply MUST be loaded right after Folder2, but it is placed in Folder5 well we’ll just add the following line:
Simply add the files and folders to the loadOrder in the order you wish these to be patched. Now that is neat!
Further reading on loadOrder I highly recommend going to the Sitecore docs.