7

I have a document library with a custom managed metadata field. I want to get the terms that are used for a particular item. I can get access to the item through the object model, and can get to the field (which by then the type is actually called a TaxonomyField), but I can't seem to get access to the terms for the field.

To be clear: I am not looking to get all possible terms for the field (I know how to do this, and there are a TON Of blogposts). I am trying to get to ONLY the terms that were picked by the user for a particular item.

3 Answers 3

9

@Ashish Patel answer does not actually work when i tried but i was able to figure it out a bit differently. Here is my version of the helper method based on example provided by code ninja blog, here:

private List<Guid> GetTermsIds(SPListItem listItem, string fieldName)
{
    var taxonomyField = listItem.Fields.GetFieldByInternalName(fieldName) as TaxonomyField;
    if (taxonomyField.AllowMultipleValues)
    {
        var fieldValuesCollection = listItem[taxonomyField.Title] as TaxonomyFieldValueCollection;
        return fieldValuesCollection.Select(x => new Guid(x.TermGuid)).ToList();
    }
    else
    {
        var fieldValue = listItem[taxonomyField.Title] as TaxonomyFieldValue;
        return new List<Guid>() { new Guid(fieldValue.TermGuid) };
    }
}
3
  • +1 this is the right way to do it, if it is unknown if there can be multiple values
    – Hinek
    Commented Jan 15, 2013 at 8:43
  • Some taxonomy fields allow multiples and others dont. Therefore, i check for that case. @Hinek, can you elaborate what you mean by "unknown"?
    – Roman
    Commented Jan 17, 2013 at 22:45
  • there are cases where the programmer already know, if the field allows multiples, then blackf0rk's solution is ok. But in most cases don't know, if the field allows multiples or not. In these cases your solution is the right way to go, so I gave it +1 ...
    – Hinek
    Commented Jan 18, 2013 at 9:58
7

This is how you do it

TaxonomyFieldValueCollection tfvc = (list.Items[0]["Field Name"] as TaxonomyFieldValueCollection);
var valuesList = (from v in tfvc select v.Label).ToList();
3
  • I also found this blog post which was quite helpful: ogarshol.blogspot.com/2010/11/…
    – blackf0rk
    Commented Jun 21, 2011 at 15:30
  • 1
    This works for fields that can hold multiple values. If you don't know if the field is set to hold multiple values or not, you should use @Roman's solution
    – Hinek
    Commented Jan 15, 2013 at 8:45
  • This is so helpful. I shortened your query by doing something like this: (items[0]["Genres"] as TaxonomyFieldValueCollection).ToList().Select(x=> x.Label); Commented Sep 24, 2013 at 21:58
1

The AllowMultipleValues property can be accessed by casting the field column as an SPFieldLookup:

SPFieldLookup tagsField = (SPFieldLookup)listToQuery.Fields["your column name"];

You need to read them through TaxonomyFieldValueCollection or TaxonomyFieldValue class depending upon value of AllowMultipleValues. Shown below

if (tagsField.AllowMultipleValues)         
{             
    TaxonomyFieldValueCollection tagsValues = new TaxonomyFieldValueCollection(tagsField);             
}         
else         
{             
    TaxonomyFieldValue tagValue = new TaxonomyFieldValue(tagsField);             
}

Then check TaxonomyFieldValue's Label and TermGuid properties.

2
  • 1
    Thanks Ashish, but this doesn't seem to work either; although the field has two tags/terms associated with it, the TaxonomyFieldValueCollection object returns 0.
    – blackf0rk
    Commented Jun 20, 2011 at 21:53
  • This can't work. You are using the Field to get the info if the field holds single or multiple values, thats right. But to get a TaxonomyFieldValue(Collection), you have to use the current value for the item (not the field, the field doesn't hold the data).
    – Hinek
    Commented Jan 15, 2013 at 8:42

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.