I'm trying to get all the keywords from a category hierarchical in Tridion 2011. I don't care to do it through the core service, from a c# TBB or a C# dll to include it into a TBB. Does anyone knows how to do it? Thanks in advance.
2 Answers
You can use the below code:
TaxonomiesOwlFilterData taxonomyFilter = new TaxonomiesOwlFilterData();
taxonomyFilter.RootCategories = new LinkToCategoryData[] {
new LinkToCategoryData { IdRef = categoryId } };
var categoryAndKeywords = _client.GetListXml(publicationId, taxonomyFilter);
Here the _client is the core service client, categoryId is the id of the category for which you need the tree and publicationId is the publication from which you want to pull the tree
Update You will have to use root keyword property and find all children keyword from there using child keyword property from there and recursively look for children keyword property till you reach end of the tree. AFAIK, there isn't any OOB call which gives hierarchical information right away. You will have to construct the tree using these 3 properties: isroot, parent keyword, child keyword
I am on mobile mode so can't provide the code for the same right now
-
I did what you proposed but I got a XML eleent into the categoryAndKeyworkds var, but the keywords I get are not hierarchial. Should I do somehitng in addition? Commented Apr 7, 2016 at 10:27
-
Sorry missed the hierarchical part of your question. Updating my answer to include some details Commented Apr 11, 2016 at 16:20
I finally got how to do it with TOM.NET. Once I have the category I do a foreach through the root keywords with a filter and I call a recursive function.
var theCategory = engine.GetObject(categoryUri) as Category;
KeywordsFilter filter = new KeywordsFilter(engine.GetSession());
filter.IsRoot = true;
foreach (Keyword key in theCategory.GetKeywords(filter))
{
if (key.GetChildKeywords().Count() != 0)
{
var lista = ObtenKeyword(key);
}
}
private List<string> ObtenKeyword(Keyword keyword, List<string> acc = null, string pila = "")
{
var childs = keyword.GetChildKeywords();
var nuevaPila = pila + "/" + keyword.Title;
var nuevoAcc = (acc == null) ? new List<string>() : new List<string>(acc);
nuevoAcc = childs.Aggregate(nuevoAcc, (sum, value) => ObtenKeyword(value, sum, nuevaPila));
if (childs.Count() == 0)
{
nuevoAcc.Add(nuevaPila);
}
return nuevoAcc;
}