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.
    1234
  3. Go to /sitecore/templates/SitecoreDemo and create a template.
    5
  4. Save the template.
  5. Switch to Visual Studio.
  6. Right click on Solution >> Manage NuGet Packages.
    6
  7. Search for Unicorn and install it. The first one.
    7
  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.
    8
  11. Move one folder back and create Serialization folder.
    9
  12. Switch back to Visual Studio.
  13. Expand the Unicorn folder (App_Config\Include\Unicorn).
    10
  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.
    11
  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.
    12
    b. Correct the path of the Content, we need to serialize
    c. Comment the predicates section for the workflows.
    13
  17. Publish the solution. And try to access the Sitecore.
  18. Once the Sitecore is loaded. Access http://<<YourInstanceName>>/unicorn.aspx
    14
  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.
    <pipelines>
    <owin.cookieAuthentication.validateIdentity>
    <processor type=”Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.ValidateIdentity.ValidateSiteNeutralPaths, Sitecore.Owin.Authentication”>
    <siteNeutralPaths hint=”list”>
    <path hint=”unicorn”>/unicorn.aspx</path>
    </siteNeutralPaths>
    </processor>
    </owin.cookieAuthentication.validateIdentity>
    </pipelines>
    15
  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.
    16
  23. Now our main 2 steps are complete, 2 more are left.
  24. Switch to Visual Studio.
  25. Go to Tools >> Extensions and Updates
    17
  26. Search for T4 toolbox.
    18
  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 – Assemblies.tt, Templates.tt and Unicorn.tt
    19
  30. The content of the each file is as below –
    Assemblies.tt – AssembliesContent
    Templates.tt – TemplatesContent
    Unicorn.tt – UnicornContent
  31. From the above files – unicorn.tt 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.
    20
  33. And add the file Templates.tt at solution level (Root)
  34. Create a folder Libraries and add below dlls from the bin folder
    (C:\inetpub\wwwroot\SitecoreDemo.sc\bin)
    Sitecore.Kernel.dll
    Sitecore.Mvc.dll
    Rainbow.dll
    Rainbow.Storage.Sc.dll
    Rainbow.Storage.Yaml.dll
    21
  35. Open Assemblies.tt file and update the path to these dlls.
    22
  36. And the last step is to configure the Templates.tt file.
  37. Open Templates.tt file and update the below based on the requirement.
    23
  38. Save the file.
  39. To your surprise, a Templates.cs file will be created under Templates.tt file and all the details of ID and field name of the template we had created will be added.
    24
  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 Templates.tt 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 @ rohan.kenkre@gmail.com. Once we are good to go, I will create an user for you. Thanks for the idea of guest blogging.. Happy Learing.. Happy Sitecoring 🙂

    Like

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s