Search code examples
tridiontridion-2011tridion-content-delivery

Creating a list of keywords in the Content Delivery


Imagine I have a content type that has two fields of type category: one is a taxonomy Author and another one is a taxonomy Topics, these two taxonomies are unrelated, the only 'thing' they may have in common is the component itself.

Now we go to the website as a visitor, then when the visitor clicks on a given Author I want to create a list with all the Topics that are present in Components that also contain the specific Author.

I know I could create a query object with a criteria containing both keywords from the different taxonomies to check if it retrieves any values, the problem is that I would need to do that for every single topic i.e. Author and Topic1, Author and Topic2, Author and Topic 3 etc, in the end it may mean tens of queries which I obviously don't want to do.

As I see it the taxonomy API won't help because both taxonomies and thefore their keywords are completely unrelated. Any alternatives?


Solution

  • Based on the comment by Ram G and therefore taking as starting point the code example in live content, I have verified that the following solution works:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Tridion.ContentDelivery.Taxonomies;
    using Tridion.ContentDelivery.DynamicContent.Query;
    using Tridion.ContentDelivery.DynamicContent;
    
    namespace Asier.Web.UI
    {
        public class TagCloud : System.Web.UI.WebControls.WebControl
        {
            protected override void Render(HtmlTextWriter writer)
            {
                TaxonomyRelationManager relationManager = new TaxonomyRelationManager();
                TaxonomyFactory taxFactory = new TaxonomyFactory();
    
                string taxonomyUriWhichIWantTheKeywordsFrom = "tcm:69-265-512";
    
                String[] componentUris = GetComponentUris();
                String[] contextKeywordUris = GetKeywordUris();
                Keyword[] contextKeywordArray = GetKeywordsFromKeywordUris(taxFactory, contextKeywordUris);
                Keyword[] cloudFacets = relationManager.GetTaxonomyKeywords(taxonomyUriWhichIWantTheKeywordsFrom, componentUris, contextKeywordArray, new CompositeFilter(), 16);
    
                ProcessKeywords(cloudFacets);
            }
    
            private static string[] GetComponentUris()
            {
                // This should probably be replaced with a Query object that
                // retrieves the URIs dynamically 
                return new String[] { "tcm:69-3645-16", "tcm:69-3648-16", "tcm:69-3651-16" };
            }
    
            private static string[] GetKeywordUris()
            {
                // this should probably be passed in as a property of the control
                return new string[] { "tcm:69-3078-1024" };
            }
    
            private static Keyword[] GetKeywordsFromKeywordUris(TaxonomyFactory taxFactory, String[] contextKeywordUris)
            {
                Keyword[] contextKeywordArray = new Keyword[contextKeywordUris.Length];
    
                for (int i = 0; i < contextKeywordUris.Length; i++)
                {
                    contextKeywordArray[i] = taxFactory.GetTaxonomyKeyword(contextKeywordUris[i]);
                }
    
                return contextKeywordArray;
            }        
    
            private static void ProcessKeywords(Keyword[] cloudFacets)
            {
                for (int i = 0; i < cloudFacets.GetLength(0); i++)
                {
    
                    if (cloudFacets[i].ReferencedContentCount > 0)
                    {
                        // Do whatever...
                    }
                }
            }
        }
    }