I was looking for a way to automatically generate Glass Models for my Sitecore Templates that had been serialized using Unicorn’s Rainbow YAML serialization formatter and came across this post  by Sitecore MVP Robin Hermanussen – http://hermanussen.eu/sitecore/wordpress/2015/04/generating-sitecore-code-without-tds

After reading the documents on GitHub it sounded perfect so I forked the repo and took a look at what the project was doing – https://github.com/hermanussen/sitecore.codegenerator

My first step were :-

  1. Updated to .Net4.6.1
  2. Used Visual Studio 2017 with T4 templates for 2017 – https://github.com/hagronnestad/T4Toolbox/releases/tag/vs2017-b1
  3. Used Sitecore Nuget Feed for Sitecore dlls.
  4. Targeting 8.2.170728 Sitecore 8.2 Update 5

Then I made some updates to the models that were being outputted using the T4 helpers from the tds-codegen project –  https://github.com/HedgehogDevelopment/tds-codegen

Changed to GlassGenerator :-

  1. Switched to Rainbow Provider
  2. Supported more Sitecore field types
  3. Changed file name to *.cs instead of *.gen.cs

Changes to GlassMappedClassTemplate :-

  1. Namespace uses the default namespace and the template location.
  2. Static Class for Template and Field Values
  3. Used static classes for SitecoreType TemplateId
  4. Used static classes for SitecoreField Name
  5. Added inheritance of IGlassBase interface

Here is the original output :-

And this is what Sitecore.CodeGenerator is outputting now with my changes:-


Have a play with the code – https://github.com/Wesley-Lomax/sitecore.codegenerator and checkout Robins repo – https://github.com/hermanussen/sitecore.codegenerator I’ve found it a great timesaver for boiler plate models for your renderings.


3 thoughts on “Sitecore Code Generation of Glass Models using serialized data with Sitecore.CodeGenerator

  1. Nice post! Looking into the code, I wasn’t able to determine how the field for “eats” mapped to an enumerable for IFood. However, I did find it in the comments.

    What would be really nice… to be able to determine that automatically.

    For instance, children types could be determined from the insert options in sitecore (__Masters field). But link types would have to be determined from parsing and applying the “Source” on each field. Apply the xpath (usually) to see where an droplink or drop tree points. You would want to determine the potential link types by determining the insert options of the item it points to.

    Treelist has a much more friendly syntax, if provided, you could determine the exact template(s) based on the “IncludeTemplatesForSelection” parameter. Determining based on xpath would be less realistic. So, the way I handle it… when needed, just include the partial with the property you need, e.g. IFood Eats {get;set;} in a path that wouldn’t be overwritten when the code regenerates. Those types come in handy but it’s difficult to determine what those types should be based on sitecore’s model. In the rare case when you need strongly typed properties to other sitecore items, it’s probably easiest to just do it once in a partial than to update your t4 templates to handle that every time. IMO YMMV etc.

      1. Yeah, I typically append “ID” or “IDs” to the field name in code generation if it’s a link type or a link list type.

        So it would be something like IEnumerable EatsIDs {get;set;}
        Then for your strongly typed property, you wouldn’t have to do property name gymnastics to get a good name, you just call it “Eats”

        IEnumerable Eats { get; set; }

        Sitecore is so open ended though. What “Eats” points to *could* be a list of IFood item, but there’s no guarantee. It’s 99% guaranteed to be list of Guids though 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *