How to get TCM URI of a category for KeywordFieldDefinitionData type fields?
Asked Answered
M

2

6

I want to fetch TCM URI of a category for KeywordFieldDefinitionData type fields.

I am using below link's code to read metadata fields of a component:-

https://code.google.com/p/tridion-practice/wiki/ChangeContentOrMetadata

I can see Category and CategoryFields properties in Reference.cs class(auto generated when refence to core service is given) but there is no property defined in Field class (defined in above code.google link) to access Category and CategoryFields properties . I have try to defined the property in following way :-

     public System.Reflection.PropertyInfo Category
    {
        get { return definition.GetType().GetProperty("Category", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic); }
    }

Even above is not working, anyone of you guys please analyse this and reply.

Thanks in advance!

Mythomania answered 25/1, 2013 at 6:24 Comment(2)
Do I understand you correctly that you want to get the ID of the Category that a Keyword is in (with the Keyword being read from a KeywordField in a Component)?Boatright
Interesting question, could we interest you in committing to the Area 51 Tridion specific proposal. Sign up with the same SO account if you have a moment.Demasculinize
R
4

You need to read the each SchemaField definition data and check if the Type is KeywordFieldDefinitionData and then get the Category information. Please see the below sample snippet.

SchemaFieldsData schemaFields = (SchemaFieldsData)_client.ReadSchemaFields(
               "tcmuriofschema", true, readOptions);
foreach (ItemFieldDefinitionData schemaField in schemaFields.Fields) {
   switch (schemaField.GetType().Name) {
      // handle other fields..
      // CategoryLink Fields
      case "KeywordFieldDefinitionData":
               KeywordFieldDefinitionData keywordTextSchemaField = (KeywordFieldDefinitionData)schemaField;
               string LinkedCategoryTitle =  keywordTextSchemaField.Category.Title;
               string LinkedCategoryId = keywordTextSchemaField.Category.IdRef;
               break;
      default:
               break;
   }
}
Roebuck answered 25/1, 2013 at 14:0 Comment(0)
F
0

I hope the below code resolved your problem

 Publication publication = GetPublication();

            TcmUri uri = new TcmUri(int.Parse(_itemId), ItemType.Category, publication.Id.ItemId);
            _session = engine.GetSession();
            Category cat = new Category(uri, _session);

            Log.Debug("the uri is " + uri);
            Log.Debug("the cat is " + cat);

            Filter filter = new Filter();
            //filter.Conditions["IsRoot"] = true; // This works with Tridion 2011 only!

            List<Keyword> keys = cat.GetKeywords(filter) as List<Keyword>;

in the above code you can get the Category information from Cat object and all keyword information from keys object

Fulviah answered 25/1, 2013 at 7:12 Comment(1)
I think the question is about Core Service not TOM.NET. Also they want to get the category URI from the keyword. This code shows getting a Category using its URIKilogram

© 2022 - 2024 — McMap. All rights reserved.