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)

 

 

0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…