How To Check if The Current Sitecore Page Is the Homepage?

When building a website where we share controls/blocks between different layouts dynamically, there will be situations when we need custom logic to determine the control layout or template the current page uses. One very common example is being able to check if a control is on the homepage, or not. In today's tutorial, I'm going to cover a few Sitecore API's to help us achieve that.

Determining the Homepage by Url

So, one very common way to figure out if you are on the homepage is to check the current Url to the homepage's Url. If both Url's match, then you are onto a winner.
var homepage = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
var isHomepage = homepage.ID.Equals(Sitecore.Context.Item.ID))
In the snippet above, I'm first using the Sitecore Context's Database.GetItem() method to get the homepage item. In the second line, we can then compare the homepage ID with the current pages ID. If the two match, then you know you are on the homepage. In case you are curious, in the background, the homepage is set within your web.config file. If you navigate to the section, you should see the following entry:

      <site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="10MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" />
From this configuration, the startItem property defines which item the GetItem() will return. Another way of checking if the current page is the homepage, is by comparing the current item's full path property against the Start Path, like so:
var isHomepage = Sitecore.Context.Item.Paths.FullPath.Equals(
    Sitecore.Context.Site.StartPath, StringComparison.OrdinalIgnoreCase)

Checking The Current Item Uses the Homepage Template

The other option to check if the current page is the homepage is to check if the current item is using the homepage template. This approach might be better in a multi-language scenario. In some situations, if you have very different content and pages for each foreign language, you may have in essence, a lot of microsites in your content tree that use the max-mind geo-location database to re-direct website users. In this instance, doing a search based on the template used to render the current item makes more sense. To do this you can compare either use:
var isHomepage = Sitecore.Context.Item.TemplateName == "Homepage";
var isHomepage = Sitecore.Context.Item.TemplateID == "6275c3e8-4d58-4f23-af6a-5a18863a24cf";
When comparing the Sitecore item against the template , you can use the template name or the template ID. If you have multiple environments, then the template ID might change between your dev and production. On the other hand, if you use the template name, you need to lock down your Sitecore templates section to prevent anyone renaming the template. If the template name got renamed, then your code will obviously break>


In today's tutorial, we've covered a few ways of checking if the current Sitecore page is the start page/ home page. If you only have a single language site, then I would recommend comparing the homepage ID against the current item ID as the safest approach> If you are working in a multi-language environment, then I suggest you use something like the template being used as it gives you a bit more flexibility.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top