2

We are planning to create an event system that will create/update pages based on a keyword's meta data when a component gets classified using the same. For example, I am creating a product which has 2 fields - Brand and Product Group - both of which come from keywords. When I create Product component and select the brand as Brand A and Product Group as PG1, I need to a) Create a Product Detail page under Brand A as well as update a product list page for brand A b) Update a product list page under Product Group PG1

I am planning to do this using an event system by attaching a metadata schema to the keywords that would capture the tcm uris of the page to be updated and Component Templates to be used.

But I am not able to figure out how I can get the meta data of the keyword from the even system. Can someone help out here?

Following is the code that I have so far, but I am not able to get the id of the keyword that the field uses from here to get it's metadata schema.

    Schema schema = (Schema) component.Session.GetObject(component.Schema.Id);
    ItemFields schemaFields = new ItemFields(schema);
    KeywordField kwField = (KeywordField)schemaFields["ProductType"]; 

Thanks, Aravind

3
  • Where did you get stuck? Keywords definitely have a Metadata method. And though I like using the event system to make life easier on authors, the page creation part of this setup is fairly easy with Page Types in Experience Manager. Nov 19, 2013 at 7:38
  • Oh and please avoid un-managed tcm uris if at all possible. You may want to content port (or even test) parts of your setup at some point. Nov 19, 2013 at 7:39
  • Have update the post with where I am. Creating page might be easy for the author, but when he does not know where it has to be created for a product group and also where it has to be added to existing pages, this might be helpful. Nov 19, 2013 at 7:55

3 Answers 3

3

In your case kwField.Value will return Keyword object. There you have Metadata property.

So your code would look something like this:

...
KeywordField kwField = (KeywordField)schemaFields["ProductType"]; 
Keyword kw = kwField.Value;
ItemFields metadataFields = new ItemFields(kw.Metadata, kw.MetadataSchema);
1

Keyword.Value: Gets the value of the single-valued field.

Keyword.Values: Gets the values of the multi-valued field.

Do this way:

Schema schema = (Schema) component.Session.GetObject(component.Schema.Id);
ItemFields schemaFields = new ItemFields(schema);
KeywordField kwField = (KeywordField)schemaFields["ProductType"]; 

// This is how you will get the single-valued field 
Keyword keyword = kwField.Value;

// This is how you will get the multi-valued field 
//List<Keyword> listOfKeywords = kwField.Values;

ItemFields metadataFields = new ItemFields(keyword.Metadata, keyword.MetadataSchema);
TextField myTextField = (TextField)metadataFields["MyTextFieldName"];
string myTextFieldValue = myTextField.Value;
0

Following code did it. You cannot start from schemaFields and retreive the metadata for the keyword that was selected. It has to be from the component itself. Thanks for your help!

            ItemFields componentFields = new ItemFields(component.Content, component.Schema);
            KeywordField field = (KeywordField)componentFields["ProductType"];
            Keyword keyword = field.Value;
            ItemFields metadataFields = new ItemFields(keyword.Metadata, keyword.MetadataSchema);
            TextField myTextField = (TextField)metadataFields["componenttemplateuri"];
            string myTextFieldValue = myTextField.Value;

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.