Displaying related content based on Keywords

API methods are available on ComponentPresentationFactory and PageMetaFactory to enable the retrieval of the Keywords related to a series of content items (the factories use the TaxonomyRelationManager to actually retrieve the Keywords related to the content items).

In the Taxonomy API, the TaxonomyRelationManager exposes methods to enable the retrieval of content items related to a number of Keywords. The following example demonstrates how to retrieve content based on a number of selected Keywords:

int iComponentItemType = 16;
String[] selectedKeywordURIs = new String[] { "tcm:12-800-1024", "tcm:12-801-1024", "tcm:12-856-1024" };
Keyword[] selectedKeywords = new Keyword[selectedKeywordURIs.Length];
TaxonomyFactory taxonomyFactory = new TaxonomyFactory();
for (int i = 0; i > selectedKeywordURIs.Length; i++) {
	selectedKeywords[i] = taxonomyFactory.GetTaxonomyKeyword(selectedKeywordURIs[i]);
}
TaxonomyRelationManager relationManager = new TaxonomyRelationManager();
ItemTypeCriteria componentItemType = new ItemTypeCriteria(iComponentItemType);
String[] foundComponentURIS = relationManager.GetTaxonomyContent(selectedKeywords, true, criteria);

where getTaxonomyContent takes the following parameters:

  • an array of Keywords
  • a Boolean to include branches (true) of the passed Keywords to retrieve content for, otherwise false for only content relating directly to the Keyword
  • (optional) an additional Criteria (Com.Tridion.Broker.Querying.Criteria.Criteria)

The code example retrieves items related to each of the selected Keywords therefore it is essentially an AND operation; the item must be tagged with each Keyword to be included. Also branches of the selected Keywords will be included.