Model Class Generator For Sitecore Habitat
Posted 09/19/2016 by Yogeshwer Sharma
Model Class Generator For Sitecore Habitat is my first contribution in association with the NTTDATA to the Sitecore Marketplace.
The Model Class Generator helps to generates model classes of Sitecore data templates from Sitecore Content Tree. This module is useful if working with Sitecore Habitat. With the help of this module we can generate model class on any location dynamically.
This is a capability that existed using some Sitecore tools (like TDS and Glass Mapper), but it's sometimes difficult to tie those pieces together in the new Habitat architecture (especially since those are proprietary tools with support and training costs). I wanted to create a new tool to make that much simpler.
Installation of Model Class Generator
- Download Model.ClassGenerator-1.0.zip package from Sitecore Marketplace
- Install the Model.ClassGenerator-1.0.zip sitecore package from Sitecore installation wizard.
- After package installations add following command to your ~/App_Config/Commands.config file.
<command name="devtools:generategmcode" type="Model.ClassGenerator.SitecoreApp.CustomItemGeneratorCommand,Model.ClassGenerator" />
- Then you will need to adjust the Class Generator Utility configuration file found here ~/App_Config/Include/ClassGeneratorUtility.config.
There are two settings you have the be concerned with to start
Base.Namespace - This will be the base namespace of the auto generated namespace for an item class. The rest of the namespace you can provide from the sitecore at the time of generating classes.
For example - We have multiple feature projects and all features will have basic namespace like Sitecore.Feature.[Feature Name] (Sitecore.Feature.Navigation). So you can provide Sitecore.Feature in Base.Namespace section of config file and add [Feature Name] (Navigation) at the time of generating class.
Base.FileOutputPath - This will be the base folder that the item class files will be written to. The rest of the file path you can provide from the sitecore at the time of generating classes.
For example - We have multiple feature projects and all features are placed on same location like D:\projects\features\[FeatureName]( D:\projects\features\ Navigation). So you can provide D:\projects\features\ in Base. FileOutputPath section of config file and add [Feature Folder Name] (Navigation) at the time of generating class.
How to Use Model Class Generator
- Right click on the template folder for which template classes are needed to be generated. And then click on tools - > Generate Model Classes.
- It will open a modal window with checkbox list of all child items of selected template folder.
- Namespace - Enter the namespace value of your class.
- Class File Path – Enter folder location where you want to generate class.
- Templates Checkbox list – Select templates to generate class.
- Click on Generate button to generate class for selected templates with given Namespace and folder location.
- Now you can check generated class with name Template.cs on the given folder location.