5

In my GlassMapper model I'm getting the children of the current page using the following declaration:

public IEnumerable<BasePage> Subpages => GetChildren<BasePage>(TemplateId);

Is there a similar method I could use to get the properties of the parent of the current page into a BasePage model? I can use the template ID of this item to get the Sitecore.Data.Items.Item parent of the page, but when I have this item I've been unable to convert it back into the BasePage model.

12

You can use the [SitecoreParent] attribute for that:

[SitecoreParent]
public virtual BasePage Parent { get; set; }

It is also mentioned in the Tutorial 17 (http://www.glass.lu/mapper/sc/tutorials/tutorial17)

| improve this answer | |
  • Yep, this is what I was looking for. Thanks! – c-bro Dec 30 '16 at 20:15
  • 1
    to get parent of parent ? – user4124 Dec 6 '18 at 15:29
1

This is what I've come up with up to this point. It's working for me, but I'm unsure if there is a more direct way to grab the parent item:

public virtual BasePage getParent()
{
    ISitecoreContext context = SitecoreContext.GetFromHttpContext();
    return context.Cast<BasePage>(Sitecore.Context.Database.GetItem(this.Id.ToString()).Parent);
}
| improve this answer | |

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.