The Tom.Net API provides a bunch of classes that can be used to read a Content Manager Item’s data such as Content data or Metadata.
To read almost any field of a Component such as: TextField, KeywordField, LinkField etc , there are corresponding classes available in the namespace Tridion.ContentManager.ContentManagement.Fields for such purpose. All such Field classes are derived from the ItemField Class, an Abstract base class for an Item’s Field representing a field in content/metadata.
Let’s see how to read Custom metadata for three Content Manager Items: Component, Page and a Keyword in a C# TBB / .Net Assembly.
Reading a Component Metadata
Component component = (Component)engine.GetObject(componentURI); ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema); TextField text = metafields["MetaDescription"] as TextField; string description = text.Value;
Reading a Pages’ Metadata:
Page page = (Component)engine.GetObject(pageURI); ItemFields metaFields = new ItemFields(page.Metadata, page.MetadataSchema); TextField text = metafields["PageSEOMetaDescription"] as TextField; string MetaDescription = text.Value;
Reading a Keyword’s Metadata:
There were occasions wherein I needed to read a Keyword’s Metadata, that particular Keyword being used to tag the Component.
The first step here is to read the Keyword item used to tag any Content Manager item such as a Component as :
Component component = (Component)_engine.GetObject(componentURI); ItemFields componentFields = new ItemFields(component.Content, component.Schema); // Use KeywordField class to read Keyword fields of a component KeywordField _keywordfield = componentFields["RecipeCategory"] as KeywordField ;
Now read the corresponding Keyword Object / Item as :
if(_keywordfield ! = null ) // Get the Keyword object/Item Keyword keyword= _keywordfield.Value;
Once you have the Content Manager Item, reading the corresponding Metadata remains same.
ItemFields metaFields = new ItemFields(keyword.Metadata, keyword.MetadataSchema); TextField text = metafields["TagSubDescription"] as TextField; string AdditionalInfo = text.Value;
And to read a Category Metadata:
Firstly, get the Keyword Item as sampled in code above. Then read the corresponding Category Item.
Category category = _keywordfield.Value.OrganizationalItem;
Once you have the Category Item, reading Metadata is simple:
ItemFields metaFields = new ItemFields(category.Metadata, category.MetadataSchema); TextField text = metafields["CategoryDescription"] as TextField; string description = text.Value;
Lastly, we can use the traditional XML way too to read such Custom Metadata:
Component component = (Component)_engine.GetObject(componentURI); string getfieldName = "MetaDescription"; string fieldValue = string.Empty; if (component != null) { foreach (XmlNode node in component.Metadata.ChildNodes) { if (node.Name.ToLower() == getfieldName.ToLower()) { fieldValue = node.InnerText; break; } } }
That’s all on querying Custom Metadata. Hope such very basic examples will get you moving. Let us now explore the ways to access such Custom Metadata in DWTs.
Reading Custom Metadata in Dreamweaver Templates (DWTs):
Well…, its the most simplest and doesn’t needs any explanation. The way we use to access any Content Fields of a Component using: Component.Fields.<CONTENT_FIELD_NAME> , a similar way exists to read any Component Metadata: Component.MetaData.<METADATA_FIELD_NAME>
An example considered is worth thousand explanations:
<img src=“@@Component.Fields.ArticleImage@@” title=“@@Component.Metadata.Title@@”
alt=“@@Component.MetaData.DefaultImageAltText@@” />
And here’s a catch . Component.<FIELD_NAME> is also used commonly in DWTs to access the fields of a component and can be either the Content Field or the Metadata field. So what will happen if a content field has the same name as a metadata field ? Which field’s value will it give ?
This is the reason why we should always stick to standard practice and use explicit declarations wherever possible i.e. Specify explicitly Component.Fields.<CONTENT_FIELD_NAME> if you want to read a content field, and Component.MetaData.<METADATA_FIELD_NAME> when metadata is to be read.
By the way, Component.<FIELD_NAME> will give the value of Content field in case a field with same name exists in Metadata fields.
Hope you enjoyed this post. Comments and Suggestions are welcome.
Nice explanations and perfect code samples!!! It was from Paper.li’s Julian paper I reached this blog and found it very helpful.
I like the way you always setup a concept and provide explanations and then the necessary Code examples to start. Keep sharing more such posts
LikeLike