Auto generating Template.cs file using Unicorn and T4 templates

Whoever is working on Helix architecture knows the importance of Template.cs file in each feature or foundation modules. Now some developers are new to Helix architecture go for a manual creation of this file which utilizes good amount of efforts and there can be errors as well because a human is involved in the creation of file. So lets learn how to automate the process of creation of Template.cs file.

Let’s demonstrate this task:

  1. Go to Sitecore >> Content Editor.
  2. Create SitecoreDemo folder under Templates, Renderings, Layouts and Media Library. Since we will work in our website folder.
  3. Go to /sitecore/templates/SitecoreDemo and create a template.
  4. Save the template.
  5. Switch to Visual Studio.
  6. Right click on Solution >> Manage NuGet Packages.
  7. Search for Unicorn and install it. The first one.
  8. Once the package is installed, we need to configure it.
  9. Before configuring, we will create one Serialization folder for the unicorn to store the YAML files.
  10. Right click on Solution >> Open Folder in File Explorer.
  11. Move one folder back and create Serialization folder.
  12. Switch back to Visual Studio.
  13. Expand the Unicorn folder (App_Config\Include\Unicorn).
  14. Remove .example extension from Unicorn.CustomSerializationFolder.config and create a copy of Unicorn.Configs.Default.example and name it Unicorn.Configs.Default.config.
  15. Open Unicorn.CustomSerializationFolder.config file. Comment the two sections highlighted and add the value “D:\Sitecore Demo\CodeBase\SitecoreDemo\Serialization” – the path of the Serialization we created earlier.
  16. Open Unicorn.Configs.Default.config file. There are total 3 changes we need to make
    a. Add the paths of the folders (SitecoreDemo) we created in the content tree.
    b. Correct the path of the Content, we need to serialize
    c. Comment the predicates section for the workflows.
  17. Publish the solution. And try to access the Sitecore.
  18. Once the Sitecore is loaded. Access http://<<YourInstanceName>>/unicorn.aspx
  19. Now try to sign in with the Sitecore credentials. It is known issue that it redirects to the same page and we are not able to access Unicorn dashboard.
  20. Add the below code snippet to the end Unicorn.CustomSerializationFolder.config file.
    <processor type=”Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.ValidateIdentity.ValidateSiteNeutralPaths, Sitecore.Owin.Authentication”>
    <siteNeutralPaths hint=”list”>
    <path hint=”unicorn”>/unicorn.aspx</path>
  21. Publish the config file and access the URL – http://<<YourInstanceName>>/unicorn.aspx
  22. Scroll down and do the initial serialization and then you will see the below screen.
  23. Now our main 2 steps are complete, 2 more are left.
  24. Switch to Visual Studio.
  25. Go to Tools >> Extensions and Updates
  26. Search for T4 toolbox.
  27. Install the extensions.
  28. Restart the Visual Studio to successfully install the extension.
  29. Now create a folder (T4 templates) in any drive. Create 3 txt files and change its extensions to .tt. Name the files –, and
  30. The content of the each file is as below – – AssembliesContent – TemplatesContent – UnicornContent
  31. From the above files – will be untouched. Rest 2 we have to configure according to our need.
  32. Create folder Codegen in the solution and add the Assemblies and Unicorn file under it.
  33. And add the file at solution level (Root)
  34. Create a folder Libraries and add below dlls from the bin folder
  35. Open file and update the path to these dlls.
  36. And the last step is to configure the file.
  37. Open file and update the below based on the requirement.
  38. Save the file.
  39. To your surprise, a Templates.cs file will be created under file and all the details of ID and field name of the template we had created will be added.
  40. Any changes made to this template or a new template created under SitecoreDemo folder will be updated in this file once we open the file and click Save.

Thank you.. Happy Learning.. Happy Sitecoring.. 🙂


3 thoughts on “Auto generating Template.cs file using Unicorn and T4 templates

  1. Pingback: Creating a Layout – Part II | Sitecore Dairies

  2. My spouse and I absolutely love your blog and find nearly all of your post’s to be exactly what I’m looking for. Do you offer guest writers to write content to suit your needs? I wouldn’t mind composing a post or elaborating on a lot of the subjects you write about here. Again, awesome site!

    Liked by 1 person

  3. Hi Nicky Cappola,

    Thank you for the kind words.. You can drop me an email regarding the topic you would like to write a blog on @ Once we are good to go, I will create an user for you. Thanks for the idea of guest blogging.. Happy Learing.. Happy Sitecoring 🙂


Leave a Reply

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

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

Facebook photo

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

Connecting to %s