Ever wondered how to get the taxonomies that a content item is assigned to whilst using the Framework API?
In the older APIs there was a method called Ektron.Cms.API.Content.Taxonomy.ReadAllAssignedCategory(). This worked fine, but what if we want to use the fabby new Framework API? Well here is how:
/// <summary>
/// Return the Taxonomy ID for a given content item. Only picks
/// the first taxonomy if there are more than one.
/// </summary>
/// <param name="contentId">Content Id</param>
/// <returns>Taxonomy ID</returns>
public long GetContentItemTaxonomyId(long contentId)
{
long taxonomyId = default(long);
var taxItemMgr = new TaxonomyItemManager();
var itemCriteria = new TaxonomyItemCriteria();
itemCriteria.AddFilter(TaxonomyItemProperty.ItemId, CriteriaFilterOperator.EqualTo, contentId);
List<TaxonomyItemData> taxonomyItemsList = taxItemMgr.GetList(itemCriteria);
if (taxonomyItemsList.Any())
{
taxonomyId = taxonomyItemsList.First().TaxonomyId;
}
return taxonomyId;
}
This method returns the Taxonomy Id but could easily be adapted to return the TaxonomyItemData object instead.
Hey, I have used this code and it has been incredibly useful. I don’t suppose you have the flip side to this? Being able to find out all of the items, or users, assigned to a given taxonomy?
Thats not a bad idea, Richard. I will put something together when I next chance. Watch this space!