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 string nodeName = 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>
        private static CatalogNode GetParentCatalogNodeCode(this PageData page)
        {
            if (BreadcrumbsFactory.IsProductPage(page))
            {
                string text = page["ec"] as string;
                if (!string.IsNullOrEmpty(text))
                {
                    Entry catalogEntry = CatalogContext.Current.GetCatalogEntry(text);
                    CatalogRelationDto catalogRelationDto = 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;
                        return CatalogContext.Current.GetCatalogNode(catalogNodeId);
                    }
                }
            }
            else
            {
                if (BreadcrumbsFactory.IsCatalogNodePage(page))
                {
                    string text2 = page["CatalogNode"] as string;
                    if (!string.IsNullOrEmpty(text2))
                    {
                        CatalogNode catalogNode = CatalogContext.Current.GetCatalogNode(text2);
                        return CatalogContext.Current.GetCatalogNode(catalogNode.ParentNodeId);
                    }
                }
            }
            return null;
        }

Comments

Popular posts from this blog

POC custom pricing provider works

EPiServer CMS 11 Useful SQL Queries - 1

EPiServer CMS 11 Useful SQL Queries - 2