2

In event code for Tridion 2011 OnPagePublishPost method, I want to get Category and Keywords value from Metadata of a Component. The metadata field in the Schema is as below:

Field Type : Text

The Checkboxes Values will be Selected from a List and Category are checked.

List Type: Checkboxes

I have created a Category called "Sections" and have added Keywords to it. Those Keywords show as checkboxes in the Component. I want to get the Keyword values from the Component. The code which I have written is :

Component compSource = null;
//page is the object of Page and is passed in the parameter.
compSource = page.ComponentPresentations[0].Component;     

ItemFields fieldsMetaCommon = new ItemFields(compSource.Metadata,
compSource.MetadataSchema); 
//Getting section keywords 
//pr_section is a field in MetaDataDesign of the schema with Type as Text and with the configuration of  values will be selected from list and Categories checked.

KeywordField fieldSection = (KeywordField)fieldsMetaCommon["pr_section"];
Keyword fieldSectionkeyword = fieldSection.Value;

In component Metadata, I have selected a category and the keywords related to it are 6 as of nowand they are checkboxes, which means that the component will be tagged with multiple keywords. I am not sure how will I loop through the keywords as they are multiple. Nickoli gave me the code but i assume it will work for only a single keyword in a category.

2 Answers 2

2

If you're using Tridion 2011 and you have the OnPagePublishPost method, then you're using the legacy TOM Event System, not the new TOM.NET based Event System. They have different APIs and one is long deprecated.

If you want to do this with TOM.NET then the code would be something like this:

Assuming you've figured out how to get your Component object in question, you would use the ItemFields API to pull the Keyword fields:

KeywordField myKewordFieldInComponent = (KeywordField)fields["MyField"];
Keyword myKeyword = myKewordFieldInComponent .Value;
if (myKeyword!= null)
{
    ItemFields myKeywordsMetaFields = new   ItemFields(myKeyword.Metadata, myKeyword.MetadataSchema);
    if (myKeywordsMetaFields.Contains("MyKeywordMetaField"))
    {
        string metafieldText = ((TextField)myKeywordsMetaFields["MyKeywordMetaField"]).Value;
    }
}
7
  • I am using TOM.NET. I will try the code and update if it works for me. Apr 10, 2014 at 8:03
  • My category has multiple keywords as those are checkboxes in the component metadata.So i need to loop through all the keywords. How can i do that? Apr 10, 2014 at 8:56
  • I have modified the question Apr 10, 2014 at 9:36
  • Instead of using .Value use .Values. Take a look at the API for these details. Intellisense in Visual Studio can also go a long way. Apr 10, 2014 at 10:05
  • I tried but it shows as issue. Intellisense does not give much idea and so i raised this. As per you I changed to values but it is giving error- Keyword fieldSectionkeyword = fieldSection.Values; Apr 10, 2014 at 10:10
0

I fixed it.

KeywordField fieldSection = (KeywordField)fieldsMetaCommon["pr_section"];
Keyword fieldSectionkeyword = fieldSection.Value;

//This fieldSectionkeyword  gave the tcm id

//The below line gave the text of the keyword.
fieldSectionkeyword.Title.ToString()
2
  • It doesn't explained, what you fixed Apr 13, 2014 at 7:43
  • @Siva- I was facing issue in getting the keyword text from a category, so if u see the above three lines, line 1 gave the tcm of the keyword, line 2 is not required but you can assume that it was the part of the solution nickoli gave, and line 3 gives the value of the keyword which i wanted.
    – Varun
    Apr 14, 2014 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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