Question

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

Was it helpful?

Solution

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
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top