private Keyword recursive(List <Keyword> keywords, string valueToLookFor) { Keyword returnValue = null; foreach (var item in keywords) { if (item.KeywordName.Equals(valueToLookFor, StringComparison.InvariantCultureIgnoreCase)) { returnValue = item; } else { if (item.HasChildren) { returnValue = recursive(item.KeywordChildren.Cast <Keyword>().ToList(), valueToLookFor); } } if (returnValue != null) { break; } } return(returnValue); }
public Dynamic.IKeyword GetKeyword(string categoryUriToLookIn, string keywordName) { //Create filter to retrieve all keywords in a taxonomy CompositeFilter compFilter = new CompositeFilter(); compFilter.DepthFiltering(DepthFilter.UnlimitedDepth, DepthFilter.FilterUp); compFilter.DepthFiltering(DepthFilter.UnlimitedDepth, DepthFilter.FilterDown); //Get keywords in taxonomy (hierarchically) IEnumerable <Keyword> taxonomy = null; try { //Ugly way to see if a taxonomy exists. Alternative is to loop through all taxonomys in Tridion and check if the categoryUriToLookIn exists... taxonomy = TaxonomyFactory.GetTaxonomyKeywords(categoryUriToLookIn, compFilter, new TaxonomyHierarchyFormatter()).KeywordChildren.Cast <Keyword>(); } catch (Exception) { //TODO: Trace return(null); } //Search in taxonomy Keyword foundKeyword = null; foreach (var currentKeyword in taxonomy) { string currentKeywordName = currentKeyword.KeywordName; if (!currentKeywordName.Equals(keywordName, StringComparison.InvariantCultureIgnoreCase)) { foundKeyword = recursive(currentKeyword.KeywordChildren.Cast <Keyword>().ToList(), keywordName); } else { foundKeyword = currentKeyword; } if (foundKeyword != null) { Dynamic.Keyword returnKeyword = new Dynamic.Keyword(); Keyword par = foundKeyword.ParentKeyword; do { Dynamic.Keyword newParentKeyword = new Dynamic.Keyword(); newParentKeyword.Id = par.KeywordUri; newParentKeyword.TaxonomyId = par.TaxonomyUri; newParentKeyword.Title = par.KeywordName; returnKeyword.ParentKeywords.Add(newParentKeyword); //Add the parentkeyword to the list par = par.ParentKeyword; } while (par != null); //Add remaining properties to the returnKeyword returnKeyword.Id = foundKeyword.KeywordUri; returnKeyword.TaxonomyId = foundKeyword.TaxonomyUri; returnKeyword.Title = foundKeyword.KeywordName; return(returnKeyword); } } return(null); }