How to read value of parent element in child of sitecore
1 answer
Yes, from the current context, you can get the parent (and all of its fields) like this:
MultilistField parentMultilistField = Sitecore.Context.Item.Parent.Fields[...];
var parentTitle = Sitecore.Context.Item.Parent["Title"];
... or go multiple levels with recursion or even a Sitecore query:
var parentItem = Sitecore.Context.Item.Axes.Select.SelectSingleItem("./ancestor-or-self::*[@@templatekey = 'sometemplate']");
If you are using a glass map, you can decorate your properties with the [SitecoreParent] or [SitecoreQuery (...)] attribute, for example:
[SitecoreParent]
public virtual MyBaseItem Parent { get; set; }
[SitecoreQuery("./ancestor-or-self::*[@@templatekey='sometemplate']", IsRelative = true)]
public virtual MyBaseItem RootItem { get; set; }
+8
source to share