6

I know how to fetch Keyword metadata using Tridion Content Delivery and how to loop through Metadata fields, but i am not able to loop through if they have multivalued meta fields.

I have notifications as multivalued RTF field as metadata of some Keyword and I am trying to read it and assign it to some custom class property. But as it is multivalue, I am not able to loop through it and convert it to List<MvcHtmlString>

public class SyllabusTemplateMeta
{
    public List<System.Web.Mvc.MvcHtmlString> Notification { get; set; }
}

SyllabusTemplateMeta syllabusMeta = new SyllabusTemplateMeta();
TaxonomyFactory taxonomyFactory = new TaxonomyFactory();
var keywordMeta =taxonomyFactory.GetTaxonomyKeyword(keywordId).KeywordMeta;
IDictionary mnd = keywordMeta.NameValues;
IEnumerator mne = mnd.GetEnumerator();
if (mne != null)
{
    while (mne.MoveNext())
    {
        DictionaryEntry de = (DictionaryEntry)mne.Current;
        NameValuePair currentMeta = (NameValuePair)de.Value;
        if (currentMeta != null)
        {
            if (currentMeta.Name.ToLower() == "notifications")
            {
                syllabusMeta.notification= new System.Web.Mvc.MvcHtmlString(currentMeta.Value.ToString());;
            }
        }
    }
}

1 Answer 1

4

Since it is a multi-valued RTF, you need to be using MultipleValues property in NameValuePair as opposed to Value.

enter image description here

I was able to get all values in a multi-valued RTF in Java using the next code snippet. The Java equivalent is getMultipleValues().

@GetMapping(value = "/getnotifications")
public @ResponseBody String getNotifications() throws Exception{   
    TaxonomyFactory taxonomyFactory = new TaxonomyFactory();
    Keyword key = taxonomyFactory.getTaxonomyKeyword("tcm:19-928-1024");
    CustomMeta customMeta =  key.getKeywordMeta();
    Map<String, NameValuePair> nameValues = customMeta.getNameValues();
    String notifications = "";

    for (Map.Entry<String, NameValuePair> entry : nameValues.entrySet()) {
        List<Object> metaValues = entry.getValue().getMultipleValues();     
        for (int index = 0; index < metaValues.size(); index++){
            notifications += metaValues.get(index);
        }
    }

    return notifications;
}

Say, you have a multi-valued RTF metadata containing two values as shown in the next image.

enter image description here

The output of the code snippet just concatenates all the metadata field values and returns it. The output of running the above snippet is shown in the last image. You should be able to use something similar in your .NET app.

enter image description here

Your Answer

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

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