Get the CatalogNode of Product Listing Page

 

There are some instances when we need to know the Parent nodes of a Page based on [catalogue] Product Listing. E.g. I have a scenario when we are setting up header menus for products. Editor can select a parent Node for display child nodes as menus. But episerver classes doesnot provide any public function to return the CatalogNode of an active Product listing page. Following piece of code can help us in these scenarios.
private stringnodeName = string.Empty;
//Is it a category listing Page
bool isCatalogNodePage = BreadcrumbsFactory.IsCatalogNodePage(CurrentPage);
//Is it a product listing page
bool isProductPage = BreadcrumbsFactory.IsProductPage(CurrentPage);
           
//Get the parent catalog node/s of this page.
if (isCatalogNodePage)
{
CatalogNode node = CurrentPage.GetParentCatalogNodeCode();
       if (node != null)
              nodeName = node.ID;
       }
       else if(isProductPage)
       {
              CatalogNode node = CurrentPage.GetParentCatalogNodeCode();
              do
              {
              if(node != null)
              {
                     if (string.IsNullOrEmpty(nodeName))
                     nodeName = node.ID;
                     else
nodeName = string.Format(“{0}|{1}”, nodeName, node.ID);
node = CatalogContext.Current.GetCatalogNode(node.ParentNodeId);
              }
              } while(node.ParentNodeId>0);
}
/// <summary>
        /// Get CatalogNode of this page
        /// </summary>
        /// <param name=”page”></param>
        /// <returns></returns>
        privatestatic CatalogNode GetParentCatalogNodeCode(this PageData page)
        {
            if(BreadcrumbsFactory.IsProductPage(page))
            {
                stringtext = page[“ec”] as string;
                if(!string.IsNullOrEmpty(text))
                {
                    EntrycatalogEntry = CatalogContext.Current.GetCatalogEntry(text);
                    CatalogRelationDtocatalogRelationDto = CatalogContext.Current.GetCatalogRelationDto(0, 0, catalogEntry.CatalogEntryId, string.Empty, new CatalogRelationResponseGroup(CatalogRelationResponseGroup.ResponseGroup.NodeEntry));
                    if(catalogRelationDto.NodeEntryRelation.Count > 0)
                    {
                        int catalogNodeId = catalogRelationDto.NodeEntryRelation[0].CatalogNodeId;
                        returnCatalogContext.Current.GetCatalogNode(catalogNodeId);
                    }
                }
            }
            else
            {
                if(BreadcrumbsFactory.IsCatalogNodePage(page))
                {
                    stringtext2 = page[“CatalogNode”] as string;
                    if(!string.IsNullOrEmpty(text2))
                    {
                        CatalogNode catalogNode = CatalogContext.Current.GetCatalogNode(text2);
                        return CatalogContext.Current.GetCatalogNode(catalogNode.ParentNodeId);
                    }
                }
            }
            returnnull;
        }