I have a class where I have a set of objects such as:

[SitecoreQuery("./*[@@templateid='" + MyItem.TemplateId + "']", IsRelative = true, IsLazy = false)]
public virtual IEnumerable<MyItem> MyItems { get; set; } 

Now I would like to add a new object which would be mapped to a specific item ID, rather than a template. I've therefore tried adding:

[SitecoreQuery("//*[@@id='{" + MySpecificItem.RootId + "}']", IsRelative = false, IsLazy = false)]
public virtual MySpecificItem MyItem { get; set; }

However Glass Mapper seems to return a null object when I run the code. I could map the object using Sitecore.Context.Database.GetItem within the constructor, however I'd prefer to use the attributes for consistency.


Can you try this?

[SitecoreNode(Id = MySpecificItem.RootId)]
public virtual MySpecificItem MyItem { get; set; }

It sounds good regarding to the source code of Glass.Mapper.

  • 1
    It works, thank you. Its a shame Glass Mapper doesn't have proper documentation.
    – user1475
    Apr 26 '17 at 9:25
  • @JonathanReez it's amazingly bad and possibly intentional
    – MStodd
    Feb 18 '20 at 17:17

Your Answer

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