What the #$*!, is this virtual catalog name mangling?

I had seen a number of queries in the catalog newsgroups regarding productids and category names in virtual catalogs. Some of the queries failed because the product ids were not used as they were supposed to in virtual catalog. This post covers the name mangling issue for product ids and category names in a virtual catalog. As I had mentioned in this article, a virtual catalog can contain products, categories and variants from one or more base catalogs. Since product ids and category names are not unique across base catalogs it can be possible that two similarly named categories from different base catalogs can end up in the same virtual catalog thereby introducing an ambiguity when accessing these items in a virtual catalog. Hence in order to ambiguously identify products and categories in a virtual catalog we introduced the concept of name mangling in virtual catalogs. What this means is that a product in a virtual catalog should be referred to as "ProductId(BaseCatalogName)" and a category in a virtual catalog should be referred to as "CategoryName(BaseCatalogName").

The following sample code demonstrates accessing products and categories in base and virtual catalogs.

CatalogContext catalogContext = CommerceContext.Current.CatalogSystem;

// Accessing Products and categories in a base catalog
ProductCatalog baseCatalog =    catalogContext.GetCatalog("BaseCatalogName");
Category baseCatalogCategory = baseCatalog.GetCategory("CategoryName");
Product baseCatalogProduct = baseCatalog.GetProduct("ProductId");

// Accessing Products and categories in a virtual catalog
ProductCatalog virtualCatalog =    catalogContext.GetCatalog("VirtualCatalogName");
// When accessing a category in a virtual catalog the category name should be in the format : CategoryName(BaseCatalogname)
string categoryName = string.Format("{0}({1})", "CategoryName", "BaseCatalogName");
Category virtualCatalogCategory = virtualCatalog.GetCategory(categoryName);
// When accessing a product in a virtual catalog the productid should be in the format : ProductId(BaseCatalogname)
string productId = string.Format("{0}({1})", "ProductId", "BaseCatalogName");
Product virtualCatalogProduct = virtualCatalog.GetProduct(productId);

Note that the recordsets and the datasets returned by the catalog system for virtual catalog items will have the product ids and category names in the mangled format. For eg the dataset returned by Category.Products method will contain the mangled names for the child products of the category in the ProductId column.