代码之家  ›  专栏  ›  技术社区  ›  user3779703

Sitecore-遍历复选框项以设置根节点

  •  0
  • user3779703  · 技术社区  · 7 年前

    我有一个结构,编辑器可以在任何时候选中项目上的一个框,将其标记为根节点。我的想法是,我可以将我们作为根点进行检查,然后左侧菜单将只显示该项及其同级项。

    同样,我可以为该站点的该区域重新定义一个新的谷歌分析ID

    有谁能帮助我使用webform方法检查当前项的选中字段,如果没有,则遍历,直到它找到该复选框并使用该位置项中的字段?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ahmed Okour    7 年前

    您可以使用此代码迭代项或其祖先,以找到选中字段作为根项的项

    public Item GetRootItem(Item item)
    {
        //Check if you reached the top of your content tree, you better replace the ID with your home page item ID, and then return the home page
        if(item.ID == Sitecore.Data.ID.Parse("{11111111-1111-1111-1111-111111111111}"))
           return null;
    
        //Replace CheckboxFieldName with your field name
        if (item["CheckboxFieldName"] != null && item["CheckboxFieldName"] == "1")
        {
            return item;
        }
        else
        {
             return GetRootItem(item.Parent);
        }
    }