1
var model = GetContextItem<ICmsModel>();

I noticed that glass doesn't return null if ICmsModel is not derived from the Context Item...

In this example, I'd expect glass to try and map it, but what if ICmsModel isn't derived from the Context Item. Is there a way glass can tell me? I thought glass would return null if it can't map, but it doesn't, the object is instantiated but the fields are null.

Does glass offer any methods to help achieve what I want?

  • What do you mean when you say "ICmsModel isn't derived from the Context Item"? – Dmytro Shevchenko Dec 13 '17 at 12:54
  • So, GetContextItem() returns the current item, which is usually the Sitecore Page Item. Each page has a template, but what if the page doesn't inherit from "ICmsModel" tempalte. I'm not too sure if it makes sense – Uchenna Okafor Dec 13 '17 at 12:56
  • No it does not. Provide some good code samples to explain what the problem is. – jammykam Dec 13 '17 at 14:37
  • Have you set a template Id on your ICmsModel and enforced the template in the config? – Richard Seal Dec 13 '17 at 14:59
3

You need to set the EnforceTemplate setting when mapping your interface to either SitecoreEnforceTemplate.Template or SitecoreEnforceTemplate.TemplateAndBase.

public enum SitecoreEnforceTemplate
{
    /// <summary>
    /// Will not enforce template check
    /// </summary>
    No,

    /// <summary>
    /// Checks only the items template
    /// </summary>
    Template,

    /// <summary>
    /// Checks the items template and any base templates
    /// </summary>
    TemplateAndBase
}

In most cases you would probably use the TemplateAndBase setting.

Here is how to do it with attribute configuration:

[SitecoreType(TemplateId = "{0EE93413-BD27-44E4-914F-4A94FBED83E9}", EnforceTemplate = SitecoreEnforceTemplate.TemplateAndBase)]
public interface ICmsModel
{
    // ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.