Dino
Dino

Reputation: 720

How to add a term to TermCollection (taxonomy field)

In sharePoint 2010, I want to set taxonomy values of a document field. The field can take multiple taxonomy terms.

I am doing it the wrong way because the cast of taxoTerms.Concat(terms) in TermCollection class fails :

    TaxonomyField taxoField = file.Item.Fields.GetFieldByInternalName(entry.Key) 
              as TaxonomyField;
    
    TaxonomySession taxoSession = new TaxonomySession(web.Site);

    TermStore store = taxoSession.TermStores[taxoField.SspId];

    TermSet termSet = store.GetTermSet(taxoField.TermSetId);

    if (taxoField.AllowMultipleValues)
    {   
        string[] taxoValues = entry.Value.Split(';');

        TermCollection taxoTerms = termSet.GetTerms(taxoValues[0], true);
                                            
        for (int j = 1; j < taxoValues.Length; j++)
        {
            TermCollection terms = termSet.GetTerms(taxoValues[j], true);

            if (terms.Count > 0)
            {
                taxoTerms = (TermCollection)taxoTerms.Concat(terms);
            }
        }

        taxoField.SetFieldValue(file.Item, taxoTerms); 
    }

Do you know how can I add terms to my TermCollection object so I can save the term values in the field ?

Upvotes: 2

Views: 6241

Answers (2)

Dino
Dino

Reputation: 720

I found my solution. Here it is :

TaxonomyField taxoField =
    file.Item.Fields.GetFieldByInternalName(entry.Key) as TaxonomyField;

TaxonomySession taxoSession = new TaxonomySession(web.Site);

TermStore store = taxoSession.TermStores[taxoField.SspId];

TermSet termSet = store.GetTermSet(taxoField.TermSetId);

if (taxoField.AllowMultipleValues)
{   
    string[] taxoValues = entry.Value.Split(';');

    TermCollection terms = termSet.GetAllTerms();

    List<string> taxonomyValueList = taxoValues.ToList<string>();

    TaxonomyFieldValueCollection fieldValues = new TaxonomyFieldValueCollection(taxoField);

    foreach (Term term in terms)
    {
        if (taxonomyValueList.Contains(term.Name))
        {
            TaxonomyFieldValue fieldValue = new TaxonomyFieldValue(taxoField);

            fieldValue.TermGuid = term.Id.ToString();
            fieldValue.Label = term.Name;
            fieldValues.Add(fieldValue);
        }
    }

    taxoField.SetFieldValue(file.Item, fieldValues);
} 

Hope it helps others.

Upvotes: 2

Vojtech Nadvornik
Vojtech Nadvornik

Reputation: 351

Here is a sample that could work:

var item = file.Item;
var taxonomyField = item.Fields.GetFieldByInternalName(entry.Key);
var values = new TaxonomyFieldValueCollection(taxonomyField);
values.PopulateFromLabelGuidPairs(entry.Value);
item[entry.Key] = values;
item.Update();

I did not test it on a life system so there can be some additional work, but I hope you get the general idea. The values in the entry.Value string have to contain the | and ; separated list of tags. If the tag does not exist you have to create it and get its id before you save it to the item.

HTH Vojta

Upvotes: 1

Related Questions