All Articles

Passing Data through Sitecore Config files

This post describes how to pass data to your pipeline processors, event handlers, etc. through the Sitecore configuration files.

Introduction

Sitecore provides a way to pass values to your custom processors, event handlers etc. through your Sitecore config patch files. The benefits of doing this are as follows:

  • Data of type Strings, Booleans, Numbers, and Lists can be sent through the config files.
  • The data sent can be set differently as per your environment.
  • Patch files can be written to add additional data/modify data to these configs as per requirements.
  • Data can be passed through environment variables/secrets.

Basic Data Types

The basic data types that can be passed are String, Boolean, and Numbers.

The config would look as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore>
  <events role:require="Standalone or ContentManagement">
   <event name="item:saved">
     <handler type="Foundation.Events.MyEventHandler, Foundation.Events" method="OnItemSaved" >
      <StringProperty>StringPropertyValue</StringProperty>
      <NumberProperty>100</NumberProperty>
      <BooleanProperty>true</BooleanProperty>
     </handler>
    </event>
   </events>
  </sitecore>
</configuration>

The data can be used in the class in the below manner:


namespace Foundation.Events
{
  public class MyEventHandler
  {
   private string StringProperty { get; set; }
   private int NumberProperty { get; set; }
   private bool BooleanProperty { get; set; }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

Lists

Lists of Strings can also be passed through the config files. Note that the list will need to be initialized in the constructor of your processor.

The config would look as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore>
  <events role:require="Standalone or ContentManagement">
   <event name="item:saved">
     <handler type="Foundation.Events.MyEventHandler, Foundation.Events" method="OnItemSaved" >
      <StringListProperty hint="list">
       <value1>StringPropertyValue1</value1>
       <value2>StringPropertyValue2</value2>
      </StringListProperty>
     </handler>
    </event>
   </events>
  </sitecore>
</configuration>

The data can be used in the class in the below manner:


namespace Foundation.Events
{
  public class MyEventHandler
  {
   private List<string> StringListProperty { get; set; }
   public MyEventHandler()
   {
     // Note that this initialization is mandatory for lists
     StringListProperty = new List<string>();
   }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

Custom Lists

Lists of other types can also be passed through the config. But, we will need to define how they need to be processed in our code.

In the below example, we will add a list of IDs. Your method needs to be passed to the hint property, as list:<your-method>.

The config would look as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore>
  <events role:require="Standalone or ContentManagement">
   <event name="item:saved">
     <handler type="Foundation.Events.MyEventHandler, Foundation.Events" method="OnItemSaved" >
      <IDListProperty hint="list:AddID">
       <value1>{4B7BE0A6-2C59-4F2F-8DC0-2EB53C8B31CE}</value1>
       <value2>{CBF94981-0AA8-49CF-9304-B36869058D60}</value2>
      </IDListProperty>
     </handler>
    </event>
   </events>
  </sitecore>
</configuration>

The data can be used in the class in the below manner:


namespace Foundation.Events
{
  public class MyEventHandler
  {
   private List<string> StringListProperty { get; set; }
   public MyEventHandler()
   {
     // Note that this initialization is mandatory for lists
     IDListProperty = new List<ID>();
   }
   public void AddID(string id)
   {
      if (ID.TryParse(id, out ID parsedID))
      {
        IDListProperty.Add(ID.Parse(parsedID));
      }
   }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

Custom Data Types

A class can be created as per your requirement and be passed to your config.

We can create a custom class as below:

using Sitecore.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Foundation.Events
{
    public class CustomConfiguration
    {
        private string StringProperty { get; set; }
        private int NumberProperty { get; set; }
        private bool BooleanProperty { get; set; }
        private List<string> StringListProperty { get; set; }
        private List<ID> IDListProperty { get; set; }
        public CustomConfiguration()
        {
            StringListProperty = new List<string>();
            IDListProperty = new List<ID>();
        }
        public void AddID(string id)
        {
            if (ID.TryParse(id, out ID parsedID))
            {
                IDListProperty.Add(ID.Parse(parsedID));
            }
        }
    }
}

The config would look as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore>
  <events role:require="Standalone or ContentManagement">
   <event name="item:saved">
     <handler type="Foundation.Events.MyEventHandler, Foundation.Events" method="OnItemSaved" >
      <CustomConfiguration type="Foundation.Events.CustomConfiguration, Foundation.Events">
       <StringProperty>StringPropertyValue</StringProperty>
       <NumberProperty>100</NumberProperty>
       <BooleanProperty>true</BooleanProperty>
       <StringListProperty hint="list">
        <value1>StringPropertyValue1</value1>
        <value2>StringPropertyValue2</value2>
       </StringListProperty>
       <IDListProperty hint="list:AddID">
        <value1>{4B7BE0A6-2C59-4F2F-8DC0-2EB53C8B31CE}</value1>
        <value2>{CBF94981-0AA8-49CF-9304-B36869058D60}</value2>
       </IDListProperty>
      </CustomConfiguration>
     </handler>
    </event>
   </events>
  </sitecore>
</configuration>

The data can be used in the class in the below manner:


namespace Foundation.Events
{
  public class MyEventHandler
  {
   private CustomConfiguration CustomConfiguration { get; set; }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

Reusing config data with ref

There may be data in your config that you may need to reuse across multiple classes. For this you can use the ref property.

The config would look as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
 <sitecore>
  <customData>
   <CustomConfiguration type="Foundation.Events.CustomConfiguration, Foundation.Events">
       <StringProperty>StringPropertyValue</StringProperty>
       <NumberProperty>100</NumberProperty>
       <BooleanProperty>true</BooleanProperty>
       <StringListProperty hint="list">
        <value1>StringPropertyValue1</value1>
        <value2>StringPropertyValue2</value2>
       </StringListProperty>
       <IDListProperty hint="list:AddID">
        <value1>{4B7BE0A6-2C59-4F2F-8DC0-2EB53C8B31CE}</value1>
        <value2>{CBF94981-0AA8-49CF-9304-B36869058D60}</value2>
       </IDListProperty>
      </CustomConfiguration>
  </customData>
  <events role:require="Standalone or ContentManagement">
   <event name="item:saved">
     <handler type="Foundation.Events.MyEventHandler, Foundation.Events" method="OnItemSaved" >
      <CustomConfiguration ref="customData/CustomConfiguration"/>
     </handler>
     <handler type="Foundation.Events.MySecondEventHandler, Foundation.Events" method="OnItemSaved" >
      <CustomConfiguration ref="customData/CustomConfiguration"/>
     </handler>
    </event>
   </events>
  </sitecore>
</configuration>

The data can be used in the class in the below manner:


namespace Foundation.Events
{
  public class MyEventHandler
  {
   private CustomConfiguration CustomConfiguration { get; set; }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

And the other class,


namespace Foundation.Events
{
  public class MySecondEventHandler
  {
   private CustomConfiguration CustomConfiguration { get; set; }
   public void OnItemSaved(object sender, EventArgs args)
   {
     // Use variables here as needed
   }
  }
}

Published Feb 29, 2024

Sitecore MVP Technology 2024-23. Web Developer with rich experience in Sitecore and ASP.NET MVC.