Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
Facepalm I'm not sure how I missed that overload. Thank you sir! –  Kyle Trauberman Sep 14 '11 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 '12 at 10:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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