Is there a way to check the template ID of a Sitecore item with glass mapper?
My business logic would do the following:
I would like to use the SitecoreContext
class, described here: http://www.glass.lu/Mapper/Sc/Documentation/ISitecoreContext
My code looks like this:
var context = new SitecoreContext();
var currentItem = context.GetCurrentItem<MyModel>();
if(HasCorrectTemplate(currentItem))
{
return currentItem;
}
return GetFallbackItem();
I don't really want to customize Glass Mapper for this, since it seems to me it should be a basic feature to check the template ID.
I can only think of using some kind of tricky query for this and I didn't find documentation about an other possibility.
You can try to use:
[SitecoreType(EnforceTemplate = SitecoreEnforceTemplate.Template, TemplateId = "{ID}")]
public class MyModel
{
...
Here is the description of the EnforceTemplate
property:
/// <summary>
/// Forces Glass to do a template check and only returns an class if the item
/// matches the template ID or inherits a template with the templateId
///
/// </summary>
public SitecoreEnforceTemplate EnforceTemplate { get; set; }
With the EnforceTemplate
property set Glass Mapper will check to see if the item being mapped matches the ID of the template defined by the SitecoreType
attribute. If it does then it returns the mapped item otherwise it skips it.