Handling Keywords

Components and other items can have Keywords. Keywords are a specific type of metadata: a closed set of strings that share a common Category.

Package example

In the package, Keywords are only stored as strings. That is, you cannot retrieve, say, the Category to which the Keyword belongs.

// Retrieve the value of the 'Hobby' field, which the underlying Schema
// defines as having Keyword values.
string hobby = package.GetValue("Component.Fields.Hobby");

Content Manager example

In the Content Manager, you can access the Keyword objects in a Component field that accepts only Keywords. From there, you can retrieve, say, the Category to which the Keyword belongs.

// Retrieve the name of the Category
// to which the Keyword stored in the 'Hobby' field belongs
Component component = (Component) engine.GetSession().GetObject(componentURI);
ItemFields fields = new ItemFields(component.Content, component.Schema);
KeywordField keywordField = fields["Hobby"] as KeywordField;
if (keywordField != null) {
	Category category = (Category) keywordField.Value.OrganizationalItem;
}