8

I'm using the following code to retrieve a list of keywords for a given category Id. I've been looking at the content delivery api documents as reference but my code doesn't work. I've tried adding log statements and found that the code doesn't even get inside the if statement. We're using Tridion 2011 SP1 and an oracle database (if that makes a difference).

    private List<string> GetKeywords(string categoryId)
    {
        // Taxonomy manager
        TaxonomyFactory factory = new TaxonomyFactory();

        // Get keywords
        Keyword cat = factory.GetTaxonomyKeywords(categoryId);

        // Check if keywords exist
        if (cat.HasChildren)
        {
            foreach (var keyword in cat.KeywordChildren)
            {
                keywordsList.Add(keyword.ToString());
            }
        }

        return keywordsList;
    }

Can anybody see where I'm going wrong or maybe point me in the right direction?

11

Further to Alberts suggestion I have followed the article he posted and have managed to read out a list of keywords, code is below.

    /// <summary>
    /// Default Category Id
    /// </summary>
    private const string DefaultCategoryId = "tcm:9-9072-512";

    /// <summary>
    /// Get story category Id from taxonomy
    /// </summary>
    private string _categoryID
    {
        get
        {
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["dynamic-stories-category-id"]))
            { 
                return ConfigurationManager.AppSettings["dynamic-stories-category-id"];
            }
            return DefaultCategoryId;
        }
    }

    /// <summary>
    /// Private taxonomy variable
    /// </summary>
    private TaxonomyFactory _taxonomyFactory;

    /// <summary>
    /// Gets taxonomy factory object
    /// </summary>
    public TaxonomyFactory TaxonomyFactory
    {
        get
        {
            if (_taxonomyFactory == null)
            {
                _taxonomyFactory = new TaxonomyFactory();
            }
            return _taxonomyFactory;

        }
    }

    public IEnumerable<Keyword> GetKeywords(string categoryUriToLookIn)
    {
        // Create filter to retrieve all keywords in a taxonomy
        CompositeFilter compFilter = new CompositeFilter();

        // 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 ex)
        {
            Log.Debug("There has been an error retrieving taxonomy: " + ex);
        }

        // Search in taxonomy
        foreach (var currentKeyword in taxonomy)
        {
            // Return keyword
            yield return currentKeyword;
        }
    }
}

And then I loop over each keyword and append it to a literal:

        foreach(var keyword in GetKeywords(_categoryID))
        {
            litFilter.Text += "<li><a href=\"#\">" + keyword.KeywordName + "</a></li>";
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.