我能否以编程方式查找与特定模板ID匹配的Sitecore项目的父项? [英] Can I programmatically find a Sitecore item's parent matching a certain template id?

查看:24
本文介绍了我能否以编程方式查找与特定模板ID匹配的Sitecore项目的父项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义Sitecore命令,我要扩展该命令以确定当前项是否有与特定模板ID匹配的父项。

单击命令/按钮时,我将可以访问当前的Item

由于escaping the FullPath for use in a query有问题,我最初的尝试是遍历父项(然后遍历该项的父项等),以确定是否有与模板匹配的项。

但是,似乎应该有更好的方法来做到这一点。

那么给定一个项目,查找它是否有属于某个模板的父项的最佳方式是什么?

推荐答案

sxa方式

 public static Item GetParentOfTemplate(this Item item, ID templateId)
    {
        if (item == null)
        {
            return null;
        }
        ItemRelationCacheValue itemRelationCacheValue = ParentOfTemplateCache.GetCacheForDatabase(item.Database.Name)?.Get(item, templateId);
        if (itemRelationCacheValue != null)
        {
            return item.Database.GetItem(itemRelationCacheValue.OutputID);
        }
        Template template = TemplateManager.GetTemplate(templateId, item.Database);
        if (template == null)
        {
            return null;
        }
        ID iD = item.ID;
        while (item != null)
        {
            Template template2 = TemplateManager.GetTemplate(item);
            if (template2 != null && template2.DescendsFromOrEquals(template.ID))
            {
                ItemRelationCacheValue value = new ItemRelationCacheValue(iD, item.Database.Name, templateId, item.ID);
                ParentOfTemplateCache.GetCacheForDatabase(item.Database.Name)?.Set(value);
                return item;
            }
            item = item.Parent;
        }
        return null;
    }

这篇关于我能否以编程方式查找与特定模板ID匹配的Sitecore项目的父项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆