Keyword Path in SDL Tridion
Asked Answered
L

2

6

Could someone please give some idea on how this can be done? This might be very simple and basics, but i couldn't figure this out.

Here is my requirement.

I have a category A with child keyword B and B got another Child Keyword C.

I want to get the exact path of selected keyword in my component template,Say for eg, if user selects keyword C, i need the value with path like A\B\C and not just as C. But Tridion always gives me the value as C and not as A\B\C . Component Schema is using "Tree" view to select the keywords.

Should I be writing dreamweaver custom functions to handle this? Or does tridion comes with some handler for this?

Any help would be highly appreciated. Thank you!

Thanks, KK

Lund answered 25/6, 2012 at 14:14 Comment(0)
G
9

As you just found out, the Tridion Keyword Hierarchy is "fake" - Keywords are stored as a flat list, not as a hierarchical list (like you would have with folders). The information about the parent and children keywords is stored in the keyword itself.

There are solutions for this - of course, for instance you can use this in a C# TBB:

Keyword keyword = new Keyword(new TcmUri("tcm:28-3368-1024"), session);
string hierarchy = keyword.Title;
bool done = false;
while(!done)
{
    if (keyword.ParentKeywords.Count > 0)
    {
        foreach (Keyword k in keyword.ParentKeywords)
        {
            hierarchy = k.Title + " > " + hierarchy;
        }
        keyword = keyword.ParentKeywords[0];
    }
    else
        done = true;
}
// Include Category
hierarchy = keyword.OrganizationalItem.Title + " > " + hierarchy;

EDIT: Updated to recursively "go up" the hierarchy. HOWEVER a keyword can have multiple parents, I'll leave that up to you to fix...

Gocart answered 25/6, 2012 at 14:51 Comment(2)
Thank you Nuno for sharing your suggestion. This really helps!Lund
If Nuno's answer helped you solve your problem, please accept it by clicking the large check mark on its left.Cantata
C
5

Keywords within a category are unique, so Tridion can safely refer to them by their name (and/or their TCM URI of course). And since a Keyword can have multiple parents, there may not be a single path leading from the root to your Keyword.

If in your situation the category can be represented as a tree, you can of course build a single path to each keyword. In that case you'll need some (C#) code that walks up the parents axis and concatenates the names. You can put this code either:

  1. in a TBB that you put into your template before the DWT OR
  2. in a custom Dreamweaver function.

Either way will work fine.

Cantata answered 25/6, 2012 at 14:45 Comment(1)
Thank you Frank for helping me out. This is really a valuable suggestion! Thanks a lot!Lund

© 2022 - 2024 — McMap. All rights reserved.