如何读取子项sitecore中的父项值

Sel*_*wyn 3 c# sitecore

有没有办法获取sitecore中子项的当前上下文的父项值.

我可以访问标准值,但不能访问当前上下文中字段的实际设置值.

如果可以实现,请告诉我.

Der*_*ker 8

是的,从当前上下文中,您可以检索父项(及其所有字段),如下所示:

MultilistField parentMultilistField = Sitecore.Context.Item.Parent.Fields[...];
var parentTitle = Sitecore.Context.Item.Parent["Title"];
Run Code Online (Sandbox Code Playgroud)

...或使用递归甚至Sitecore查询向上遍历多个级别:

var parentItem = Sitecore.Context.Item.Axes.Select.SelectSingleItem("./ancestor-or-self::*[@@templatekey = 'sometemplate']");
Run Code Online (Sandbox Code Playgroud)

如果您使用的是玻璃映射器,则可以使用[SitecoreParent]或[SitecoreQuery(...)] attribue来装饰属性,如下所示:

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

[SitecoreQuery("./ancestor-or-self::*[@@templatekey='sometemplate']", IsRelative = true)]
public virtual MyBaseItem RootItem { get; set; }
Run Code Online (Sandbox Code Playgroud)