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 also add the SitecoreInfoType.TemplateId
attribute to a property on your model which Glass will then map to the TemplateID of the item.
//Returns the template ID of the item as type System.Guid.
[SitecoreInfo(SitecoreInfoType.TemplateId)]
public virtual Guid TemplateId{ get; set; }
You can then check the template id against your item
if(currentItem.TemplateId == {guid-of-template-to-match})
{
return currentItem;
}
The solution from @Maras is cleaner but it depends on the set up of your templates and may depend on whether you are using Code Generation templates using TDS for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With