Search results item Primary Parent Category

Hi there

I am trying to get the primary parent category an item belongs to when returned in a search result. I find however that these always seem to be null.

Is there a way to get the primary parent category an item belongs to in a search result as I need to create a link to the category in which the product exists for each item in the search result. (info: I am using the CS 2007 starter site)


CatalogContext catalogContext = CommerceComponents.Helpers.CatalogHelper.CatalogContext;
CatalogSearch catalogSearch = catalogContext.GetCatalogSearch();

// Select
catalogSearch.SearchOptions.ClassTypes = CatalogClassTypes.ProductClass |
CatalogClassTypes.ProductFamilyClass |
CatalogClassTypes.ProductFamilyForVariantsClass;

catalogSearch.SearchOptions.SortProperty = "cy_list_price";
catalogSearch.SearchOptions.SortDescending = true;
catalogSearch.SearchOptions.PropertiesToReturn = SiteContext.Configuration.PropertiesToReturn;
catalogSearch.FreeTextSearchPhrase = @"Harry";

// From
catalogSearch.CatalogNames = @"AU DD CM Base Catalog";
int totalRecords = 0;
CatalogItemsDataSet catalogItems = catalogSearch.Search(out totalRecords);

Response.Write("Total Records: " + totalRecords.ToString());

foreach (CatalogItemsDataSet.CatalogItem catalogItem in catalogItems.CatalogItems)
{
Response.Write(catalogItem.ProductId.ToString());
Response.Write(catalogItem.PrimaryParentCategoryName.ToString());

Response.Write(catalogItem["ItemNumber"].ToString() + " - " + catalogItem["AuthorName"]);
}

[2018 byte] By [Ryan..] at [2008-1-7]
# 1

Hello Ryan,

Did you try using catalogItem.ParentCategories? This returns a CategoryCollection.

The previous call returns a null possibly because the catalog did not contain that data.

Ning

NingLin-MSFT at 2007-10-2 > top of Msdn Tech,Commerce Server,Commerce Server 2007...
# 2
Hi Ning

Thanks heaps for the reply, sorry I am a bit of a newbie at this, I cannot seem to find the catalogItem.ParentCategories Category Collection.

Can you please advise where this resides.

Many thanks

Ryan

Anonymous at 2007-10-2 > top of Msdn Tech,Commerce Server,Commerce Server 2007...
# 3

Hi Ryan

What is SiteContext.Configuration.PropertiesToReturn set to in the code above? If PropertiesToReturn does not contain "PrimaryParentCategory" property the value for that property will not be returned by search.

Thanks

Sudha

SudhaRaghavan-MSFT at 2007-10-2 > top of Msdn Tech,Commerce Server,Commerce Server 2007...
# 4
Properties to return is set to..

CategoryName,PrimaryParentCategory,ProductID,CatalogName,i_ClassType,DisplayName,cy_list_price,AuthorName,ItemNumber,BlockWeb,Artist,ShortDescription,Image_filename,ClubPrice,VIPPrice,Price,SpecialPrice,BudgetPrice,BonusPrice,StarPrice,RRP,SaveUpTo

With this I have also tried catalogItem["PrimaryParentCategory"] which returns a null and also catalogItem["CategoryName"] which returns a null

Ryan.. at 2007-10-2 > top of Msdn Tech,Commerce Server,Commerce Server 2007...
# 5
If I added the below code into the "foreach" loop, will this have a significant impact on my search result speed ?

Product tempProduct = catalogContext.GetProduct(catalogItem.CatalogName, catalogItem.ProductId);
if (tempProduct.ParentCategories.Count > 0)
{
Response.Write(tempProduct.ParentCategories[0].DisplayName);
}
Ryan.. at 2007-10-2 > top of Msdn Tech,Commerce Server,Commerce Server 2007...