Retrieve the SiteInfo for an item in multi-site setup

There are various blog posts out there that offer different solutions to retrieve the site information for a Sitecore item but the ones that I came across lacked support for multi-site setup where multiple site configurations have the same root path and start item. This is common when your Sitecore solution supports multi-lingual content.

Here is my extension method that caters for multi-lingual, multi-site setups:

public static SiteInfo GetSite(this Item item)
   List<SiteInfo> siteInfoList = Factory.GetSiteInfoList();
   SiteInfo currentSiteinfo = null;
   int matchLength = 0;
   foreach (SiteInfo siteInfo in siteInfoList)
      string startPath = string.Concat(siteInfo.RootPath, siteInfo.StartItem);
      if (item.Paths.FullPath.StartsWith(startPath, StringComparison.OrdinalIgnoreCase)
         && siteInfo.Language.Equals(item.Language.Name, StringComparison.OrdinalIgnoreCase) && startPath.Length > matchLength)
         matchLength = startPath.Length;
         currentSiteinfo = siteInfo;

   return currentSiteinfo;

Happy coding!

Leave a Reply

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