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

As a Sitecore developer definitely we will come across this scenario where we want to go to the root item that is the home item. In certain situation, we may come across where we need to check if the current Sitecore page is homepage. In this tutorial let’s look at How To Check if The Current Sitecore Page Is the Homepage.

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

The easiest way is to write a simple code and see if the current URL is pointing to the home page URL.

var homepage = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
var isHomepage = homepage.ID.Equals(Sitecore.Context.Item.ID))

This first line is to fetch the home item in the Sitecore and the second line actually used to compare if the current page or current item is a home item or not. It returns true if the item is a home page else false.

We always set the default path of the home item in the web.config and hence the code Sitecore.Context.Site.StartPath always returns home item.

<sites>
  <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" />
</sites>

Another way of verifying if the current page is homepage or not is by comparing the current item’s full path with the home items path i.e., start path.

var isHomepage = Sitecore.Context.Item.Paths.FullPath.Equals(
   Sitecore.Context.Site.StartPath, StringComparison.OrdinalIgnoreCase)

 

 

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Javascript

Javascript URLEncode

Let us take a look at what would be the best approach for Encoding URL using Javascript. There are basically 3 Javascript URLEncode methods available. Types of Javascript URLEncode escape()…
View Post