0 out of 3 rated this helpful - Rate this topic

TaxonomyFieldValue Class

SharePoint 2010

Represents a single value held in a TaxonomyField object.

System.Object
  Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue

Namespace:  Microsoft.SharePoint.Taxonomy
Assembly:  Microsoft.SharePoint.Taxonomy (in Microsoft.SharePoint.Taxonomy.dll)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class TaxonomyFieldValue
using System;
using System.IO;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace Microsoft.SDK.SharePointServer.Samples
{
    // This code requires a reference to the Microsoft.SharePoint.dll and Microsoft.SharePoint.Taxonomy.dll
    internal partial class TestTaxonomy
    {
        /// <summary>
        /// This method creates a taxonomy field value for the given taxonomy field with a value of the given term
        /// </summary>
        /// <param name="term">Value to set</param>
        /// <param name="taxonomyField">Field that value is for</param>
        private void TestTaxonomyFieldValueBasic(Term term, TaxonomyField taxonomyField)
        {
            if (term == null)
            {
                throw new ArgumentException("Parameter term cannot be null");
            }
            if (taxonomyField == null)
            {
                throw new ArgumentException("Parameter taxonomyField cannot be null");
            }

            // If you have a term use the following:
            TaxonomyFieldValue taxonomyValue = new TaxonomyFieldValue(taxonomyField);
            taxonomyValue.TermGuid = term.Id.ToString();
            taxonomyValue.Label = term.Name;

            Console.WriteLine("TaxonomyFieldValue created with value " + taxonomyValue.ToString());
        }


        /// <summary>
        /// This method shows how a TaxonomyFieldValue can be created from the results of a TaxonomyWebTaggingControl
        /// </summary>
        /// <param name="text">Text from taxonomy web tagging control which will provide a value</param>
        /// <param name="taxonomyField">Field that value is for</param>
        private void TestTaxonomyFieldValueBasicFromWebControl(string text, TaxonomyField taxonomyField)
        {
            if (taxonomyField == null)
            {
                throw new ArgumentException("Parameter taxonomyField cannot be null");
            }
                       
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("a value must be picked from the web tagging control");
            }

            TaxonomyFieldValue taxonomyValue = new TaxonomyFieldValue(taxonomyField);
            taxonomyValue.PopulateFromLabelGuidPair(text);

            Console.WriteLine("TaxonomyFieldValue created with value " + taxonomyValue.ToString());
        }

        /// <summary>
        /// This method shows how to create TaxonomyFieldValue that is not bound to a field
        /// </summary>
        /// <param name="term">Value to set</param>
        private void TestTaxonomyFieldValueNoField(Term term)
        {
            if (term == null)
            {
                throw new ArgumentException("Parameter term cannot be null");
            }

            // When this constructor is used calling ValidatedString will throw an exception
            TaxonomyFieldValue taxonomyValue = new TaxonomyFieldValue("-1" + // We don't know the WssId so default to -1
                ";#" + // This is the lookup field delimiter
                term.Name +  // the label you want the TaxonomyFieldValue to have
                TaxonomyField.TaxonomyGuidLabelDelimiter + // This is the delimiter between the Label and Guid
                term.Id.ToString()); // This is the ID you want the TaxonomyFieldValue to have

            Console.WriteLine("TaxonomyFieldValue created with value " + taxonomyValue.ToString());
        }
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Above didnt work for me so had to do it the same as in this post
http://blogs.msdn.com/b/kaevans/archive/2012/02/15/updating-sharepoint-managed-metadata-columns-with-powershell.aspx
Doesn't provide localized labels
If you try to get the Label programmatically from this class, it doesn't return the localized label value based on the user's current language selection from the MUISelector.

It looks like you have to grab the giud of the term, create a taxonomy session object, find the Term, and find the matching label from the Term for that LCID.
CAML query using a TaxonomyFieldValue

If you would like to know how you can use this value to query items using CAML, see this post:
http://pholpar.wordpress.com/2010/02/03/some-words-about-taxonomyfieldvalue-and-its-wssid-property/