Getting templateID of glass generated interface

26. januari 2017 08:18 by martijn in

If you are using glass with the t4 template genaration you can generate partial interfaces for your Sitecore datasource templates. These interfaces look a something like this: 

I create a simple helper to get the ID like this

  

And here's the implemenation:

public static class GlassHelper

{

        public static Guid GetTemplateId<T>() where T : ISitecoreBase

        {

           var attribute = typeof(T).GetCustomAttributes(typeof(SitecoreTypeAttribute), true).FirstOrDefault() as SitecoreTypeAttribute;

           return !string.IsNullOrEmpty(attribute?.TemplateId) ? 

               new Guid(attribute.TemplateId) : 

              Guid.Empty;

        }

}