Search code examples
tridiontridion-2011

How to get full category path from a keyword in Tridion


Can any one help me to get full category path from a given keyword. I am giving one example as below,

Example:

Category 1----> Keyword 1 -----> Keyword 11,

say from metadata i got the value "Keyword 11", but i need whole path i.e. /Category 1/ Keyword 1/Keyword 11.

Can anyone help me how to achieve this in Template Building Block using c#.


Solution

  • Below code should help you to get the path.

    bool isRecursive = false;
    KeywordField kwdField = (KeywordField)metaFields["kwdField"];
    Keyword curKwd = new Keyword(kwdField.Value.Id, engine.GetSession());
    string kwdPath = curKwd.Title;
    while (!isRecursive) {
      if (curKwd.ParentKeywords.Count > 0){
         foreach (Keyword kwd in curKwd.ParentKeywords) {
            kwdPath = kwd.Title + "/" + kwdPath;
         }
         curKwd = curKwd.ParentKeywords[0];
      } else {
         isRecursive = true;
      }
    }
    kwdPath = curKwd.OrganizationalItem.Title + "/" + kwdPath;