TaxonomyFieldValue Class
SharePoint 2010
Represents a single value held in a TaxonomyField object.
Namespace:
Microsoft.SharePoint.Taxonomy
Assembly: Microsoft.SharePoint.Taxonomy (in Microsoft.SharePoint.Taxonomy.dll)
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());
}
}
}
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
- 2/26/2012
- Pr1nz
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.
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.
- 9/24/2010
- Adam Toth
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/
- 2/3/2010
- Peter Holpar