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

855 Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

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;
   }
}
1
On

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