Skip to main content

Use a patch file to customize the Sitecore configuration

Abstract

How to use patch files to add or change server configuration settings.

You use patch files to add or change configuration settings in the Sitecore.config file. XM Cloud merges the patch files with the Sitecore.config file to create the configuration file that is used at runtime.

This topic describes how to:

To create a patch file to customize configuration settings:

  1. Create a file in one of the two customizable layers in :

    • The Custom layer – you must place patch files that customize default XM Cloud settings in a way that is independent of the system environment in the /App_Config/Include folder, which belongs to the Custom layer.

    • The Environment layer – you must place patch files that configure XM Cloud for a specific environment, such as QA or Development, in the /App_Config/Environment folder, which belongs to the Environment layer.

    You can place the file either in the root of the folder or in a subfolder that you create within the folder.

  2. Give the patch file a name ending with the extension .config.

  3. Start with this basic structure in the file:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <sitecore>
    </sitecore>
    </configuration>
    
  4. In the <sitecore> section of the patch file, add the setting changes that you want to add to the configuration.

    Note

    You can only patch settings in the app_config/sitecore.config file. Settings in other files such as the web.config and app_config/layers.config files cannot be patched. If you need to change the settings in these files, you must edit them directly.

When XM Cloud applies the changes from a patch file, it tries to match each element in the patch file to an element in the existing configuration using the combination of the element name and all the attributes of the element. If there is a match, XM Cloud updates the existing element. If there is no match, XM Cloud inserts a new element.

Note

If a patch file element matches more than one element in the existing configuration, XM Cloud applies the update to the first matching element that it finds. We therefore strongly recommend that you specify enough attributes of the element to uniquely identify it.

By default, XM Cloud inserts new elements at the end of the configuration section that the elements belong to.

To control how XM Cloud inserts elements:

  1. Register the patch and set XML namespaces that enable you to use attributes to modify or add settings, by adding them to the <configuration> setting:

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" 
    xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    
  2. Use a patch: attribute to specify where the element is placed relative to existing elements, or to specify that it replaces or updates an existing element of the same name. You can use the following attributes:

    • patch:before – inserts the element before the specified element.

    • patch:after – inserts the element after the specified element.

    • patch:attribute – defines or replaces the specified attribute of the element. You can achieve the same effect using the set namespace.

    • patch:delete – removes the specified element.

    • patch:instead – replaces the specified element.

    For example, to add a site named mysite and place it before the existing site with the name website, use the following syntax:

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <sites>
          <site patch:before=”*[@name='website']” name=”mysite” ... />
        </sites>
      </sitecore>
    </configuration>
    

Note

You can use examples of patch files to help you change the XM Cloud configuration.

The order in which XM Cloud loads configuration files is important because the changes that a file makes can be modified or overwritten by files that load later. If XM Cloud finds the same configuration setting in multiple files, the last file to load overwrites the previous versions of the setting.

To control the sequence in which the configuration files load within a layer:

  1. In the /App_config/layers.config file, add a <loadOrder> setting to the layer's definition:

    <layer name="Custom" includeFolder="/App_Config/Custom/">
    <loadOrder>
    </loadOrder>
    </layer>
    
  2. Add the file folders or individual files inside the <loadOrder> section in the order you want them to load:

    <loadOrder>
    <add path="Folder23" type="Folder" />
    <add path="Folder9/sitespecific.config" type="File" />
    <add path="Folder1" type="Folder" />
    </loadOrder>
    

When XM Cloud encounters a <loadOrder> setting, it loads the configuration files in this order:

  1. Folders and files that are specified in the <loadOrder> section load first, in the order they are listed. In the previous example, the files in the Folder23 folder load first, then the sitespecific.config file, and then the files in the Folder1 folder.

  2. Folders and files that are not specified in the <loadOrder> section load second. They load in alphabetical order. Files that are placed in the root of a folder load before files within subfolders.