Home » Customization » Kick-start Glass.Sitecore.Mapper in a Sitecore Initialize Pipeline

Kick-start Glass.Sitecore.Mapper in a Sitecore Initialize Pipeline

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.


In my previous post, I used Glass.Sitecore.Mapper to grab content out of Sitecore for use in expanding tokens set in Standard Values fields.

While writing the code for that article, I recalled Alex Shyba asking whether it were possible to move Glass initialization code out of the Global.asax and into an initialize pipeline — Mike Edwards, the developer of Glass, illustrates how one initializes Glass in the Global.asax on github.

I do remember Mike saying it were possible, although I am uncertain whether anyone has done this.

As a follow up to Alex’s tweet, I’ve decided to do just that — create an initialize pipeline that will load up Glass models. I’ve also moved the model namespaces and assembly definitions into a patch config file along with defining the initialize pipeline.

Here is the pipeline I’ve written to do this:

using System.Collections.Generic;
using System.Linq;

using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Pipelines;

using Glass.Sitecore.Mapper.Configuration.Attributes;

namespace Sitecore.Sandbox.Pipelines.Loader
    public class InitializeGlassMapper
        public void Process(PipelineArgs args)

        private static void CreateContextIfApplicable(IEnumerable<string> modelTypes)
            if (CanCreateContext(modelTypes))

        private static bool CanCreateContext(IEnumerable<string> modelTypes)
            return modelTypes != null && modelTypes.Count() > 0;

        private static AttributeConfigurationLoader CreateNewAttributeConfigurationLoader(IEnumerable<string> modelTypes)
            Assert.ArgumentNotNull(modelTypes, "modelTypes");
            Assert.ArgumentCondition(modelTypes.Count() > 0, "modelTypes", "modelTypes collection must contain at least one string!");
            return new AttributeConfigurationLoader(modelTypes.ToArray());

        private static void CreateContext(AttributeConfigurationLoader loader)
            Assert.ArgumentNotNull(loader, "loader");
            Glass.Sitecore.Mapper.Context context = new Glass.Sitecore.Mapper.Context(loader);

        private static IEnumerable<string> GetModelTypes()
            return Factory.GetStringSet("glassMapperModels/type");

I’ve defined my new initialize pipeline in a patch config, coupled with Glass model namespace/assembly pairs:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor type="Sitecore.Sandbox.Pipelines.Loader.InitializeGlassMapper, Sitecore.Sandbox" />
      <type>Sitecore.Sandbox.Model, Sitecore.Sandbox</type>

On the testing front, I validated what I developed for my previous post still works — it still works like a charm! 🙂



  1. Mike, do you know which Sitecore code runs the initialize pipeline? Does it run on every page request or only once per application start? I was digging in the Sitecore Kernel yesterday and couldn’t find the pipeline run call that runs this pipeline.


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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: