Generate Glass Mapper interfaces using T4 templates without TDS
Switch branches/tags
Clone or download
Latest commit 2763d84 Aug 4, 2017


This project allows you to generate interfaces/classes based on your Sitecore template structure. It works with serialized data from Sitecore (incl. TDS/Unicorn) and uses T4 templates for generating the files. The T4 Toolbox is used to generate multiple files.

By default, it supports generating interfaces that are decorated with Glass Mapper attributes. But the Sitecore.CodeGenerator project only provides easy access to the serialized template information and can therefore easily be used to create T4 templates for other mapping/wrapping frameworks.

How it works

Sitecore.CodeGenerator reads serialized data from the filesystem. The T4 templates do the rest of the work.

  • - The central class that actually uses Sitecore.CodeGenerator.DLL to read the serialized data and then creates the individual generated files.
  • - Individual template for what the generated files should look like. If you need to customize the code that needs to be generated, this is the first place to look.
  • - You can make as many copies of this as you like, for different groups/folders of templates. The generated files will show up as children of this item in the Solution Explorer.

Adding code generation to your project

  1. Install the T4 Toolbox Visual Studio extension.
  2. Compile Sitecore.CodeGenerator or download it here. Copy the DLL to a folder that is within reach from your project (no need to add a reference; a dependency within your project isn't needed).
  3. Copy the .tt files and include them in your project. The files are, and
  4. For and, empty the project property "Custom Tool". Also, change the file references at the top of these files to correctly reference the Sitecore.Kernel.DLL, Sitecore.CodeGenerator.DLL and Glass.Mapper.Sc.DLL files.
  5. In, change the following code so the path references your serialized data:Context.Host.ResolvePath(@"..\Data\serialization\"). The path is relative to the location of the .tt files.
  6. In, change the following code to set the path(s) of the templates that you want to generate code for: new [] { "/sitecore/templates/Sitecore Code Generator Sample" }.
  7. Run the code generation either by right-clicking and choosing "Run Custom Tool", or choosing "Build" > "Transform All T4 Templates".

Using Unicorn's Rainbow format

Since Unicorn has adopted a new serialization format from version 3, the code generation needs to be configured slightly different to be used with that.

  1. Go to and locate the RunCore() method
  2. Locate the following line within that method: var resolver = new TemplatesResolver(
  3. Change that line to the following: var resolver = new TemplatesResolverRainbow(
  4. Ensure that the path to the serialized data (as set in step 5 of the previous section) points to a valid Unicorn configuration that contains Sitecore templates in .yml files. Note that this is on the next line of code.

Further configuration

It can't always be determined exactly from the Sitecore data what the return types of properties need to be. E.g.: A multilist may be used to select a number of items of a certain type. But you need to define that type yourself.

By default, the mapping of field types to C# types is done by in the GetFieldOptions(...) function. You can change it there.

But for individual scenario's like the one described earlier (the multilist that maps to items of a certain type), you can use a specific technique in the file Simply put, you make exceptions for individual cases, by checking a field ID and then setting the field options. E.g.:

// Add custom options, like setting the return type of a field property explicitly

// Dog -> Food
	fieldOptions.GlassFieldTypeName = "IEnumerable<IFood>";


Copyright (C) 2015-2016 Robin Hermanussen

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see