Tridion Core Service - Work with Hierarchical Taxonomy

I use Tridion Core Service (Tridion 2011 SP1) to get a list of keywords for a given category identifier.

CoreService2010Client client = new CoreService2010Client(); XElement xmlCategoryKeywords = client.GetListXml(category.Id, new KeywordsFilterData()); 

This returns what appears to be a flat XML structure representing our taxonomy, which has 4 depth levels.

The documentation describes an approach to working with this:

 var categoryKeywords = xmlCategoryKeywords.Elements().Select(element => element.Attribute("ID").Value).Select(id => (KeywordData)client.Read(id, null) ); foreach (KeywordData keyword in categoryKeywords) { Console.WriteLine("\t Keyword ID={0}, Title={1}", keyword.Id, keyword.Title); } 

However, this will only be a list of each keyword. The KeywordData object contains the ParentKeywords property, so you could create a hierarchy in memory.

Is it possible to get XML from a hierarchical core service? Or an easier way to work with this data?

+4
source share
3 answers

One way is to use TaxonomiesOwlFilterData strong>:

 string publicationId = "tcm:0-3-1"; var filter = new TaxonomiesOwlFilterData(); filter.RootCategories = new[] {new LinkToCategoryData{ IdRef = "tcm:3-158-512"},}; var list = ClientAdmin.GetListXml(publicationId, filter); 

As you can see, it is called upon publication, but you can narrow it down to one or more categories. It will return you a scary XML list, which you can continue as follows:

 XNamespace tcmc = publicationId + "/Categories#"; XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; XNamespace tcmt = "http://www.tridion.com/ContentManager/5.2/Taxonomies#"; var taxonomyTree = new Dictionary<string, List<string>>(); var keywordNodes = list.Descendants(tcmc + "cat"); foreach (var keywordNode in keywordNodes) { var parents = new List<string>(); var parentNodes = keywordNode.Descendants(tcmt + "parentKeyword"); if (parentNodes.Count() > 0) { foreach (var parentNode in parentNodes) { parents.Add(parentNode.Attribute(rdf + "resource").Value); } } taxonomyTree.Add(keywordNode.Attribute(rdf + "about").Value, parents); } 

As a result, you will get an unordered list of your keywords and relevant parents, which you can process as you wish. An item that does not have a parent is the parent keyword. This may not be the most beautiful solution, but at least you only need one server call and not read every keyword.

+4
source

You can process each branch, level by level. Here is some code that I played with this does this:

 CoreService2010Client client = new CoreService2010Client("basicHttp_2010"); KeywordsFilterData keywordsDataFilter = new KeywordsFilterData() { BaseColumns = ListBaseColumns.IdAndTitle, IsRoot = true }; UsingItemsFilterData usingItemsFilter = new UsingItemsFilterData() { BaseColumns = ListBaseColumns.IdAndTitle, ItemTypes = new[] { ItemType.Keyword }, InRepository = new LinkToRepositoryData() { IdRef = "tcm:0-1-1" } }; XElement parents = client.GetListXml("tcm:1-272-512", keywordsDataFilter); foreach (XElement parent in parents.Descendants()) { // Do something with the parent (top level) KW XElement children = client.GetListXml(parent.Attribute("ID").Value, usingItemsFilter); foreach (XElement child in children.Descendants()) { // Do something with the child KW } } 

In the past, I found that processing a flat list in a hierarchy (in my case, listing all SGs in a publication) created huge overhead compared to processing a branch at a time. Of course, I should warn about this by saying that I tried it with the old (early version 5.x) Tridion, so the situation has improved since then.

+1
source

Tridion 2011 SP1 comes with a new version of CoreService EndPoint. CoreService 2011. It is recommended that you use the last endpoint. The last endpoint has new functionalists and bug fixes. SP1 also has a default coreservice client proxy that you can use directly in your code.

+1
source

Source: https://habr.com/ru/post/1399345/


All Articles