It is possible to automatically generate Sitecore templates just coding models? I'm using Sitecore 8.0 and I saw Glass Mapper Code First approach but I cant find more information about that.
-
TDS can generate code for you, the model. See http://hedgehogdevelopment.github.io/tds/chapter6.html – Jan Bluemink Jun 13 '16 at 06:30
-
1@JanBluemink I'm pretty sure the OP wants it the other way round. He wants to code his model first and then generate Sitecore templates from the model classes. – Dmytro Shevchenko Jun 13 '16 at 06:50
-
Exactly i need to code model first and then generate Sitecore templates – Makler Jun 13 '16 at 07:15
-
Sitecore Rocks had this functionaliy but they freeze it in the last versions... https://www.youtube.com/watch?v=amUIYY06J3Q – Vlad Iobagiu Jun 13 '16 at 08:03
-
1What's the reason that you want to use code first template generation? I believe it makes more sense to build your templates in Sitecore and let Glass mapper generate the code for you using TDS code generation... It automatically sets your field types and stuff.. Maybe there's another solution to your problem. – Bas Wiebing Jun 13 '16 at 08:18
-
Yes i know TDS code generation is a good solution, but i had task to try code first approach using Glass Mapper – Makler Jun 13 '16 at 08:38
2 Answers
Not sure why there isn't much info about it, but you can definitely model/code first!. I do it alot using the attribute configuration approach like so:
[SitecoreType(true, "{generated guid}")]
public class ExampleModel
{
[SitecoreField("{generated guid}", SitecoreFieldType.SingleLineText)]
public virtual string Title { get; set; }
}
Now how this works. The SitecoreType 'true' value for the first parameter indicates it may be used for codefirst. There is a GlassCodeFirstDataprovider which has an Initialize method, executed in Sitecore's Initialize pipeline. This method will collect all configurations marked for codefirst and create it in the sql dataprovider. The sections and fields are stored in memory. It also takes inheritance into account (base templates).
I think you first need to uncomment some code in the GlassMapperScCustom class you get when you install the project via Nuget. The PostLoad method contains the few lines that execute the Initialize method of each CodeFirstDataprovider.
var dbs = global::Sitecore.Configuration.Factory.GetDatabases();
foreach (var db in dbs)
{
var provider = db.GetDataProviders().FirstOrDefault(x => x is GlassDataProvider) as GlassDataProvider;
if (provider != null)
{
using (new SecurityDisabler())
{
provider.Initialise(db);
}
}
}
Furthermore I would advise to use code first on development only. You can create packages or serialize the templates as usual and deploy them to other environment so you dont need the dataprovider (and potential risks) there.

- 1,155
- 5
- 10
You can. But it's not going to be Glass related.
Code first is exactly what Sitecore.PathFinder is looking to achieve. There's not a lot of info publicly available on this yet however.
Get started here: https://github.com/JakobChristensen/Sitecore.Pathfinder

- 5,829
- 1
- 22
- 33