Sitecore Code Generation of Glass Models using serialized data with Sitecore.CodeGenerator

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 –

After reading the documents on GitHub it sounded perfect so I forked the repo and took a look at what the project was doing –

My first step were :-

  1. Updated to .Net4.6.1
  2. Used Visual Studio 2017 with T4 templates for 2017 –
  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 –

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 – and checkout Robins repo – I’ve found it a great timesaver for boiler plate models for your renderings.