gpt4 book ai didi

sitecore - 如何使用 glass.mapper 获取项目的模板 ID?

转载 作者:行者123 更新时间:2023-12-03 22:28:17 26 4
gpt4 key购买 nike

有没有办法使用玻璃映射器检查 Sitecore 项目的模板 ID?

我的业务逻辑将执行以下操作:

  • 获取上下文项
  • 如果上下文项具有特定模板,则可以
  • 如果它具有不同的模板,则使用该模板查找另一个项目
    根据一些业务规则也检查模板

  • 我想用 SitecoreContext类,此处描述: http://www.glass.lu/Mapper/Sc/Documentation/ISitecoreContext

    我的代码如下所示:

    var context = new SitecoreContext(); 

    var currentItem = context.GetCurrentItem<MyModel>();

    if(HasCorrectTemplate(currentItem))
    {
    return currentItem;
    }

    return GetFallbackItem();

    我真的不想为此自定义 Glass Mapper,因为在我看来它应该是检查模板 ID 的基本功能。

    我只能想到为此使用某种棘手的查询,而我没有找到有关其他可能性的文档。

    最佳答案

    您还可以添加 SitecoreInfoType.TemplateId属性到您的模型上的属性,然后 Glass 将映射到项目的 TemplateID。

    //Returns the template ID of the item as type System.Guid.
    [SitecoreInfo(SitecoreInfoType.TemplateId)]
    public virtual Guid TemplateId{ get; set; }

    然后,您可以根据您的项目检查模板 ID
    if(currentItem.TemplateId == {guid-of-template-to-match})
    {
    return currentItem;
    }

    @Maras 的解决方案更清晰,但它取决于您的模板设置,并且可能取决于您是否使用 TDS 的代码生成模板。

    关于sitecore - 如何使用 glass.mapper 获取项目的模板 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35646312/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com