Dzianis Yafimau
Dzianis Yafimau

Reputation: 2016

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

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

Upvotes: 2

Views: 3424

Answers (2)

Marek Musielak
Marek Musielak

Reputation: 27132

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.

Upvotes: 3

Derek Dysart
Derek Dysart

Reputation: 1396

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
}

Upvotes: 6

Related Questions