Code Generation for Sitecore Unicorn

Code Generation for Sitecore Unicorn

I recently made the switch from TDS to Unicorn (and you should too, it is great). One of the things I was missing was the code generation that TDS provided. I didn’t want to start manually generating constant files for template GUIDs and field names again – so I wrote a small utility based on the Visual Studio T4 templates.

Introducing: RainbowCodeGeneration

Rainbow is the underlying serialisation provider that Unicorn uses and RainbowCodeGeneration is a (very!) simple utility that reads Rainbow files (at the moment this is limited to the YAML serialisation format) and a T4 template that  generates code based on your Sitecore templates. Here’s what the outcome looks like when you use the out-of-the-box T4 template in the Sitecore Habitat News module:


Each template creates a struct with the template ID (both as a Sitecore ID and as a string). Each field within the template is a nested struct with the field name as a string. The C# XML comments provide extra support when referring to these constants (e.g. by providing the full Sitecore path and the field type). This is based on the TDS code generation templates.

Adding Unicorn Code Generation to your Sitecore Project

Simply install the RainbowCodeGeneration package using NuGet.

Note: Depending on your Project setup, you might see an error (E.g. missing assembly for Sitecore.Kernel.dll). The current T4 base template targets Sitecore Habitat but can simply be changed for your configuration.

Adjust a few settings on the T4 template to make it run in your configuration.

  • Ensure the assembly references at the top of the file resolve correctly (especially the path to your Sitecore.Kernel.dll, line 9 in the T4 template)
  • The physicalFileStore setting needs to point to where you store the Unicorn / Rainbow items for your project. The out of the box setting uses the relative paths that Habitat uses.
  • The treeName setting is the name of the sub-tree in Unicorn. This is the name of the include on your predicate in Unicorn. The “News” feature in Habitat would use “Feature.News.Templates”
  • The treePath setting is the matching path in Sitecore. The “News” feature in Habitat would use “/sitecore/templates/Feature/News”


The code generation for your Sitecore templates will run when you save the T4 template.

You can tweak the T4 template to your liking. The entire IItemData object from Rainbow for the template and its fields is available within the template.

Re-Generate Code

When your Unicorn items update, you will have to re-run the code generation. In Visual Studio, right-click on the T4 template and select “Run Custom Tool” to re-run the code generation.

Version 0.1.1 released

RainbowCodeGeneration is open source and the first version (0.1.1) is now available on GitHub and NuGet.

One thought on “Code Generation for Sitecore Unicorn

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.