using System;
using System.Linq;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;
namespace ASPNet3346
{
public partial class GetCatalogCategories : System.Web.UI.Page
{
protected void btnCS2007_Click(object sender, EventArgs e)
{
ProductCatalog productCatalog = (ProductCatalog)CommerceContext.Current.CatalogSystem.GetCatalog("Adventure Works Catalog", "en-US");
// Get the root category.
Category rootCategory = productCatalog.GetRootCategory();
CatalogItemsDataSet categories = rootCategory.ChildCategories.DataSet;
foreach (CatalogItemsDataSet.CatalogItem category in categories.CatalogItems)
{
DisplayHelper.DisplayValue("DisplayName",category.DisplayName,Response);
}
}
private void GetRootCategory()
{
var catalogQuery = new CommerceQuery("Catalog");
//catalogQuery.SearchCriteria.Model.Properties["language"] = "en-US";
catalogQuery.SearchCriteria.Model.Id = "Adventure Works Catalog";
//Get Root Category
// Add Related Query Operation for root Category ie. _root_
{
CommerceQueryRelatedItem queryRootCategory =
new CommerceQueryRelatedItem("RootCategory", "Category");
queryRootCategory.Model.Properties.Add("Id");
queryRootCategory.Model.Properties.Add("DisplayName");
catalogQuery.RelatedOperations.Add(queryRootCategory);
}
CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);
// Handle Responses
CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();
DisplayHelper.DisplayValue("Catalog ID", productCatalog.Id, Response);
DisplayHelper.DisplayValue("IdentifyingProductProperty", productCatalog.Properties["IdentifyingProductProperty"], Response);
var rootCategory = (productCatalog.GetPropertyValue("RootCategory") as CommerceRelationship).Target;
DisplayHelper.DisplayValue("Category ID", rootCategory.Id, Response);
DisplayHelper.DisplayValue("Category DisplayName", rootCategory.Properties["DisplayName"].ToString(), Response);
}
protected void btnCS2009_Click(object sender, EventArgs e)
{
// GetRootCategory(); // Optional for your knowledge
var catalogQuery = new CommerceQuery("Category");
catalogQuery.SearchCriteria.Model.Properties["DisplayName"] = "_root_";//this indicates its root category, Please refer GetRootCategory() for more details
catalogQuery.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog";
//Get Child Categories
// Add Related Query Operation for child products
{
CommerceQueryRelatedItem queryChildProducts =
new CommerceQueryRelatedItem("ChildCategories", "Category");
catalogQuery.RelatedOperations.Add(queryChildProducts);
}
CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);
// Handle Responses
CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();
var childCategories = productCatalog.GetPropertyValue("ChildCategories") as CommerceRelationshipList;
foreach (var commerceRelationship in childCategories)
{
CommerceEntity childcategory = commerceRelationship.Target;
DisplayHelper.DisplayValue("Root Category ID", childcategory.Id, Response);
}
}
}
}
Attached Source Code : ASPNet3346


