Reading custom Sitecore configuration

While writing out some Sitecore functionality, we often need to add new Sitecore settings. Basic setting fields can be easily added in the sitecore section.

These settings can be retrieved in code using the methods provided in Sitecore.Configuration.Settings, eg.

Settings.GetIntSetting("SitecoreModules.ImageLazyLoadResizeModule.Offset", 200)

If you have a more complex structure of settings in mind, you might be better off creating your own custom xml structure within the Sitecore configuration settings mode.

Here’s an example

<configuration xmlns:patch="">
      <DesktopBreakpointInfo CssClass="data-responsive-desktop" Breakpoint="0" ImageMaxWidth="580"  />
      <TabletBreakpointInfo CssClass="data-responsive-tablet" Breakpoint="980" ImageMaxWidth="350"  />
      <MobileBreakpointInfo CssClass="data-responsive-mobile" Breakpoint="768" ImageMaxWidth="200"  />

To read these settings i created a couple of POCO classes

    public class Breakpoint
        public string Name { get; set; }
        public string CssClass { get; set; }
        public string BreakpointPx { get; set; }
        public int ImageMaxWidth { get; set; }

    public class BreakpointsConfig
        public Breakpoint MobileBreakpoint { get; set; }
        public Breakpoint TabletBreakpoint { get; set; }
        public Breakpoint DesktopBreakpoint { get; set; }

Following code allowed us to populate the settings

using Sitecore.Configuration;
using Sitecore.Xml;
using SitecoreModules.ImageLazyLoadResizeModule.Models;
using System.Web;
using System.Xml;

namespace SitecoreModules.ImageLazyLoadResizeModule.Helpers
    public class BreakpointConfigHelper
        public static BreakpointsConfig GetBreakpointInfoConfig()
                BreakpointsConfig breakpointsConfig = new BreakpointsConfig();
                breakpointsConfig.DesktopBreakpoint = GetConfigFromNode(Factory.GetConfigNode("SitecoreModules.ImageLazyLoadResizeModule/DesktopBreakpointInfo"));
                breakpointsConfig.TabletBreakpoint = GetConfigFromNode(Factory.GetConfigNode("SitecoreModules.ImageLazyLoadResizeModule/TabletBreakpointInfo"));
                breakpointsConfig.MobileBreakpoint = GetConfigFromNode(Factory.GetConfigNode("SitecoreModules.ImageLazyLoadResizeModule/MobileBreakpointInfo"));


       private static Breakpoint GetConfigFromNode(XmlNode node)
            if (node == null) return null;

            int imageMaxWidth;
            if (!int.TryParse(XmlUtil.GetAttribute("ImageMaxWidth", node), out imageMaxWidth))
                return null;

            return new Breakpoint
                Name = XmlUtil.GetAttribute("Name", node),
                CssClass = XmlUtil.GetAttribute("CssClass", node),
                BreakpointPx = XmlUtil.GetAttribute("Breakpoint", node),
                ImageMaxWidth = imageMaxWidth

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s