Programmatically Setting Taxonomy Field Values

by Marcel Medina7. October 2010 11:02

Hi folks,

This week I have performed the migration of Lookup Lists and Lookup fields. So far I am really impressed by the lack of documentation and examples in how to work with Taxonomy field values.

In this quick note I want to show you guys an example of how to work with the new object model for the migration of field values (Lookup fields X Taxonomy fields).


Consider the following Lookup List and Term Store in this example, that contains the World Cup 2010 Brazilian football players, according to Figure 1:

vs680  Figure 1 – Lookup List vs Taxonomy Term Store

Note: As you can see, the decision in migrating Lookup fields to Taxonomy fields is a battle. Does it worth your efforts? In my case as I am creating a Content Type Hub, all the Site Columns must not have dependencies, so a solution is to get rid of Lookup fields.

Lookup fields X Taxonomy fields

Imagine a List that contains both fields, the former maps the Lookup List and the latter the Term Store. In this scenario the Lookup field contains values, on the other hand the Taxonomy field does not, because it was recently created and needs to be filled out.

As a solution for that, the code below maps the Lookup field values into Taxonomy field values, aka Managed Metadata field values.

Code Snippet
  1. static void Main(string[] args)
  2. {
  3. var url = ConfigurationManager.AppSettings["Url"].ToString();
  4. var library = ConfigurationManager.AppSettings["Library"].ToString();
  6. // Reads XML, considering
  7. // (key => lookup field,value => taxonomy field)
  8. Dictionary<string, string> mappingFields = XMLHelper.ReadDictionaryXML("MappingFields.xml");
  10. using (SPSite site = new SPSite(url))
  11.     {
  12. using (SPWeb web = site.RootWeb)
  13.         {
  14.  // Gets the list/library
  15.  SPList list = web.Lists[library];
  17.  foreach (SPListItem item in list.Items)
  18.             {
  19.  // Iterates through all mapped fields
  20.  foreach (var mappedField in mappingFields)
  21.                 {
  22.  if (item.Fields.ContainsField(mappedField.Key))
  23.                     {
  24.  // Allows updates without a trace
  25.                         web.Site.AllowUnsafeUpdates = true;
  27.  // Gets the lookup field instance
  28.  var lookupValueList = (item[mappedField.Key] as SPFieldLookupValueCollection).ToList();
  30.  // Gets the taxonomy field instance
  31.  TaxonomyField managedField = item[mappedField.Value] as TaxonomyField;
  33.  // Gets the current taxonomy session
  34.  TaxonomySession session = new TaxonomySession(web.Site, false);
  36.  // Gets the term store (by SspId)
  37.  var termStoreCol = session.TermStores[managedField.SspId];
  39.  // Gets the terms of a specific term set (by TermSetId)
  40.  var termCol = termStoreCol.GetTermSet(managedField.TermSetId).Terms;
  42.  var listTerms = new List<Term>();
  44.  // Iterates through the lookup values
  45.  foreach (var itemValue in lookupValueList)
  46.                         {
  47.  string value = itemValue.LookupValue;
  49.  // Gets the correspondent term for the each value
  50.  // found in the lookup values list
  51.  var termToSet = termCol[value];
  53.                             listTerms.Add(termToSet);
  54.                         }
  56.  // Sets the field value using the list of terms
  57.                         managedField.SetFieldValue(item, listTerms);
  59.  // Persists the item
  60.                         item.SystemUpdate();
  62.  // Denies further unsafe updates
  63.                         web.Site.AllowUnsafeUpdates = false;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }

Note: The code above is very well commented, so no comments.

I hope it helps.



Marcel Medina

Click here to read the same content in Portuguese.

Tags: ,

Quick Notes

Comments (5) -

10/26/2010 1:33:01 PM #

brian jank

i've got a metadata field which allows multiple values, but that setFieldValue method overwrites any single existing term in the field, I couldn't find a method which would allow me to add a second value without eliminating the first, Do you happen to know off hand how I would do that?

brian jank  | Reply

10/26/2010 10:42:20 PM #


Hope this link will help you.

RameshUSA  | Reply

10/30/2010 6:14:16 AM #

Marcel Medina

When checking the TaxonomyField class using the Object Browser in Visual Studio, you can see there is just the SetFieldValue method for setting values (in 8 different signatures).

Marcel Medina  | Reply

10/27/2010 9:49:41 PM #

Charles Chen


Couldn't figure out how to set taxonomy field types in Word documents.  Turns out that you can set them in SharePoint just fine using the serialized value (<string>|<guid>), but to push those changes down to the document itself, you need to use TaxonomyField.SetFieldValue(...)

Thanks for the post.

Charles Chen  | Reply

2/9/2012 7:45:47 PM #


Can you please post your MappingFields.xml?

Brent  | Reply

Add comment

  Country flag

  • Comment
  • Preview


Marcel Medina is a specialist in .NET/SharePoint development.

Currently working as Microsoft Consultant and Trainer in these technologies.

He has 13 years of experience in the IT area and the following certifications: MCP, MCDBA, MCAD, MCSD, MCTS (including WSS 3.0, MOSS 2007 & SharePoint 2010, Application Development), MCITP (SharePoint Administrator 2010), MCPD (Enterprise Application Developer 2.0/3.5 & SharePoint Developer 2010), MCSD SharePoint Applications and MCT.

Support this site

If you want to keep this site running, please donate:








Month List

Site Meter