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 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.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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();
share|improve this answer
    
I also found this blog post which was quite helpful: ogarshol.blogspot.com/2010/11/… –  blackf0rk Jun 21 '11 at 15:30
    
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 Jan 15 '13 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); –  The Muffin Man Sep 24 '13 at 21:58

@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) };
    }
}
share|improve this answer
    
+1 this is the right way to do it, if it is unknown if there can be multiple values –  Hinek Jan 15 '13 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 Jan 17 '13 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 Jan 18 '13 at 9:58

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.

share|improve this answer
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 Jun 20 '11 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 Jan 15 '13 at 8:42

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.