2

I want to programmatically set the values of the Enterprise Keywords Managed Metadata field. This field supports multiple values, but I can only get it to take one at a time.

I have the following method:

public void AddKeywords(SPListItem item, string keywordString)
{
    TaxonomyField managedField = item.Fields.GetFieldByInternalName(Fields.Keywords) as TaxonomyField;

    if (managedField != null)
    {
        TaxonomySession session = new TaxonomySession(item.Web.Site, false);
        TermStore termStore = session.TermStores[managedField.SspId];
        TermSet termSet = termStore.GetTermSet(managedField.TermSetId);

        string[] keywords = keywordString.Split(',').Select(s => s.Trim()).ToArray();

        foreach (string keyword in keywords)
        {
            Term keywordTerm;
            if (!termSet.Terms.Any(t => t.Name == keyword))
            {
                keywordTerm = termSet.CreateTerm(keyword, CultureInfo.CurrentCulture.LCID);
                termStore.CommitAll();
            }
            else
            {
                keywordTerm = termSet.Terms[keyword];
            }

            managedField.SetFieldValue(item, keywordTerm);
        }
    }
}

After this is executed, only the last keyword is set at the value, overwriting any previous values on this field.

How can I set multiple values to the Enterprise Keywords Managed Metadata Field?

0

1 Answer 1

4

In your example you are setting the value of the field, then overwriting it when your loop gets to the next keyword.

Rather than calling managedField.SetFieldValue every time you should be collecting all your terms in some form of collection, then calling it with this overload : TaxonomyField.SetFieldValue

for example

    var terms = new List<Term>();

    foreach (string keyword in keywords)
    {
        Term keywordTerm;
        if (!termSet.Terms.Any(t => t.Name == TaxonomyItem.NormalizeName(keyword)))
        {
            keywordTerm = termSet.CreateTerm(keyword, CultureInfo.CurrentCulture.LCID);
            termStore.CommitAll();
        }
        else
        {
            keywordTerm = termSet.Terms[keyword];
        }

        terms.Add(keywordTerm);
    }

    managedField.SetFieldValue(item, terms);

Another completely unrelated suggestion I would make is call TaxonomyItem.NormalizeName on your keyword before comparing it, or keywords with & will complicate things.

2
  • Facepalm I'm not sure how I missed that overload. Thank you sir! Sep 14, 2011 at 23:42
  • I tried the above code, it is working perfectly for multiple values but if i add a new value, the value is overwritten in the enterprise keyword column, it is not adding the value to the existing list. Any suggestions?
    – user8147
    May 2, 2012 at 10:52

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.