1

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.

1 Answer 1

1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.