How to read value of parent element in child of sitecore

Is there a way to get the value of the parent of the current context of the child in the sitecore.

I can access the default values, but not the actual set value of the field in the current context.

Please let me know if this can be achieved.

+3


source to share


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







All Articles