How to determine in sitecore whether given item is start item?

邮差的信 提交于 2019-12-24 05:05:39

问题


In config file we set start item for each website in element (e.g. startItem="/Home"). And we also can select start item in code. But what I am asking about is how to determine for any selected item whether it is start item or not?

At least we can select start item and compare with given item, but it is not elegant code I think


回答1:


We typically have an extension method on the SiteContext class to get the Home Item:

public static class SiteExtensions
{
    public static Item GetHomeItem(this SiteContext site)
    {
        return Sitecore.Context.Database.GetItem(site.StartPath);
    }
}

With this you can test any item (not just the Context item) to see if it's the home item.

Item home = Sitecore.Context.Site.GetHomeItem();

if (Sitecore.Context.Item.ID == home.ID) 
{
    // Context item is the home item
}



回答2:


Just from the top of my head:

bool isStartItem = item.Paths.FullPath.Equals(
    Sitecore.Context.Site.StartPath, StringComparison.OrdinalIgnoreCase)

I support there may be cleaner solution but this one works and is fast.

Remember that in multisite solutions for one site your item can be a start item while for another site sane item doesn't have to be a start item.



来源:https://stackoverflow.com/questions/25037501/how-to-determine-in-sitecore-whether-given-item-is-start-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!