如何在Sitecore中获取父项或任何其他项的子yout参数

本文关键字:其他 任何 参数 yout Sitecore 获取 | 更新日期: 2023-09-27 18:25:00

我做了一些研究,但最终没有找到可能的解决方案。以下是我能够找到的主题:

  • Sitecore:如何使用codebehind中的subyout参数
  • https://markstiles.net/Blog/2011/04/27/sitecore-sublayout-parameters-and-datasources.aspx

所有人都在暗示。。。

var sublayout = ((Sublayout)this.Parent);
NameValueCollection nvc = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);

是正确的,但我想要更多,因为这个方法只能给我当前上下文子布局,即我所在的Sitecore中的当前页面。我想要可以检索特定项的子布局参数的代码。例如,如果我使用路径获取项目(页面),那么如何获取其子布局参数?

PS:我使用的是Sitecore 7.0

如何在Sitecore中获取父项或任何其他项的子yout参数

简单的答案是您无法在页面上获取另一个子yout的子yout参数。

这是因为在向该子层发出请求并将参数添加到管道中的请求之前,参数不会被传递(如果我没记错的话,参数是在管道中传递的,而不是在上下文中传递的——不管怎样,这对两者都适用)。

因此,获取不同子层参数的唯一方法是:1.如果您在父子系统中,并且正在查看您在子子系统控件的代码中设置的参数2.如果你在进入Sitecore时经历了一些过于乏味的代码(这可以做一些你可以更容易地用另一种方式做的事情),那么在Presentation Details中获取添加到页面的所有子布局,并查看每个子布局的参数。但是,请注意,此解决方案仅为您提供在Sitecore中设置的参数。

两种解决方案都不会为您提供在另一个子系统的代码后面设置的参数

第二种解决方案不是一个好的解决方案,因为它会很混乱和过于复杂。如果你真的需要像这样共享参数数据,你应该做的是扩展Sitecore上下文(这可能对你正在做的事情来说仍然比需要的更多),或者使用Session来共享数据(这可能是对你来说最好的解决方案,但要确保你没有过度使用它;就像任何事情一样,在开始使用它之前,先研究Session)。

编辑

如果您正在寻找有关传入请求的参数的进一步信息,则需要反编译Sitecore.Kernel.DLL并手动进行调查。这方面没有任何文档,只是在某个地方的博客文章中隐藏了一两颗宝石。

关于您关于编写"一个调用请求并检索参数的函数"的评论,我强烈建议不要这样做。如果我们充分了解您的用例,这是一个非常丑陋的解决方案,很容易被更好的解决方案取代。

编辑2

在你的评论中,你说:

我有一个项目树,其中一些项目可能有,也可能没有具有所需的参数(例如复选框)。A->B->C->D,如果我在D页,我想迭代到父节点,直到我发现复选框被选中。

从这条评论中,我认为您错误地使用了"parameters"一词来代替"fields"。字段是添加到模板中的控件(如Signle Line Text字段、Rich Text字段、Checkbox字段、Multilist字段等),在模板上给定标题,并且可以由用户设置。相反,Parameter是以与查询字符串相同的语法传递给子层的键值对。参数是在演示详细信息中设置的,而字段是在项本身(或其__Standard值或继承的__Standard值)上设置的。

如果我是正确的,而你滥用了"参数"这个词,那么你根本不想做你所描述的事情。你想做的就是:

// looking for the youngest item from the context item through its ancestors that has the "Foo" check box checked
var youngestItemWithFoo = Sitecore.Context.Item; //give the found item a name
bool foundYoungestChecked; //will be our loop condition/flag to show we found the youngest that has the box checked
// we need the root path to ensure that we do not go too far up the tree
var rootPath = Sitecore.Configuration.Factory.GetSite("website").RootPath;
// Foo is a checkbox field - the below checks raw values to see if foo is checked.
// ** Note that the way that this condition is written, the root item (often the "Home" item)
//    will still be checked but the loop will not traverse farther up the tree. You may need
//    to change this to stop the traversal sooner, depending on your solution **
while (!(foundYoungestChecked = youngestItemWithFoo["Foo"] == "1") && youngestItemWithFoo.Paths.Path != rootPath) 
{
    youngestItemWithFoo = youngestItemWithFoo.Parent; // if the Foo field is not checked, look at the parent and continue
}
//make sure that an item was found, as it is possible that none were
if (foundYoungestChecked) 
{
    //...item was found - do your stuff...
} 
else 
{
   //...item was not found - do some other stuff...
}

我过去也做过类似的事情来查找父项的渲染。它涉及到围绕LayoutDefinition和RenderingDefinition类包装的大量代码。关键路径是获取布局定义,然后获取设备,然后获取该设备的渲染图。类似这样的东西:

Sitecore.Layouts.LayoutDefinition layout = Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]); 
var defaultDeviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}";
var device = layoutDefinition.GetDevice(defaultDeviceId);
var renderingDefinitions = device.GetRenderings(renderingId);
foreach(var renderingDefinition in renderingDefinitions){
   string parameters = renderingDefinition.Parameters;
}

我假设一个项目和子YouTube的ID(renderingId)对您可用。还请注意,我有一个用于默认设备的硬编码设备ID,用于此示例。您可能需要在多个设备上循环,或者为特定设备配置配置项。看看John West关于更新布局细节的博客,了解如何访问这些数据。

相关文章: