How to fetch the content Using Broker Query in Tridion
We can use create a 2 helper class files i.e. one is Broker.cs and Provider.cs where we can call these 2 functions anywhere and at any time in order to fetch all kinds of data from Broker Query.
For e.g.: am explaining few simple scenarios like how to fetch Categories/Keywords etc. using Broker Query which majority of application uses the same.
1. This is will give the List of all the Keywords which are present in the Category called as “Product” where am passing this tcm-id (i.e. 47-512) to SitelabelConfiguration.cs which this will passed as a JSON Object while fetching the category or else directly you can hardcode this tcm id in query it-self but it’s not a good Practice.
In Provider.cs:
public List<ProductListModel> GetAllProducts()
{
List<ProductListModel> productModelList = new List<ProductListModel>();
try
{
string productTemplateUri = String.Format("{0}-512", SiteLabelConfiguration.TaxonomyProductUri);
Keyword keywords = Query.BuildKeywordProductCriteria("productTemplateUri");
foreach (Keyword item in keywords.KeywordChildren)
{
ProductListModel product = new ProductListModel();
product.ProductName = item.KeywordDescription;
product.ProductID = item.KeywordUri;
productModelList.Add(product);
}
}
catch (Exception ex)
{
throw new DxaException("Error while Getting the Products Keywords", ex);
}
return productModelList;
}
In Broker.cs:
public Keyword BuildKeywordProductCriteria(string taxonomyProductUri)
{
int id = Convert.ToInt32(WebRequestContext.Localization.LocalizationId);
string productUri = "tcm:" + id.ToString() + "-" + taxonomyProductUri;
TaxonomyFactory factory = new TaxonomyFactory();
Keyword keywords = factory.GetTaxonomyKeywords(productUri);
return keywords;
}
2. If we want to show the Child elements i.e. Child Keywords with in the same Category: Just we need add few more lines in Broker.cs File:
public List<Keyword> BuildAllProductKeywordCriteria(string taxonomyProductUri)
{
List<Keyword> keywordList = new List<Keyword>();
int id = Convert.ToInt32(WebRequestContext.Localization.LocalizationId);
string prodUri = "tcm:" + id.ToString() + "-" + taxonomyProductUri;
TaxonomyFactory factory = new TaxonomyFactory();
Keyword keywords = factory.GetTaxonomyKeywords(prodUri);
if (keywords.KeywordChildren != null)
{
foreach (Keyword key in keywords.KeywordChildren)
{
if (key.KeywordChildren.Count > 0 )
{
foreach (Keyword item in key.KeywordChildren)
{
keywordList.Add(item);
}
}
keywordList.Add(key);
}
}
return keywordList;
}
}
}
3. If we want to Show the Child Categories i.e. with-in Main Category i.e. for e.g.:
For this we need to tweak the code and here is the below one:
public List<ProductListModel> GetAllProducts()
{
List<ProductListModel> productModelList = new List<ProductListModel>();
try
{
Keyword keywords = Query.BuildKeywordProductCriteria("47-512");
foreach (Keyword item in keywords.KeywordChildren)
{
ProductListModel product = new ProductListModel();
List<ProductListModel> childProductsList = new List<ProductListModel>();
product.ProductName = item.KeywordDescription;
product.ProductID = item.KeywordUri;
if (item.KeywordChildren.Count > 0)
{
product.GroupName = item.KeywordDescription;
productModelList.Add(product);
foreach (Keyword childItem in item.KeywordChildren)
{
ProductListModel childProduct = new ProductListModel();
childProduct.ProductName = childItem.KeywordDescription;
childProduct.ProductID = childItem.KeywordUri;
if (childItem.KeywordChildren.Count > 0)
{
childProduct.GroupName = childItem.KeywordDescription;
productModelList.Add(childProduct);
foreach (Keyword anotherChildItem in childItem.KeywordChildren)
{
ProductListModel anotherChildProduct = new ProductListModel();
anotherChildProduct.ProductName = anotherChildItem.KeywordDescription;
anotherChildProduct.ProductID = anotherChildItem.KeywordUri;
anotherChildProduct.GroupName = childItem.KeywordDescription;
productModelList.Add(anotherChildProduct);
}
}
else
{
childProduct.GroupName = item.KeywordDescription;
productModelList.Add(childProduct);
}
}
//product.ChildProducts = childProductsList;
}
else
{
//product.ChildProducts = null;
product.GroupName = null;
productModelList.Add(product);
}
//productModelList.Add(product);
}
}
catch (Exception ex)
{
throw new DxaException("Error while Getting the Products Keywords", ex);
}
return productModelList;
}