SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Programmatically Setting Taxonomy Field Values

Setting Taxonomy Field values can be trick, specially because of the lack of documentation. Here I show an example of how to work with the new object model for the migration of field values (Lookup fields X Taxonomy fields).

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

Assumption

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();
  5.  
  6.     // Reads XML, considering
  7.     // (key => lookup field,value => taxonomy field)
  8.     Dictionary<string, string> mappingFields = XMLHelper.ReadDictionaryXML("MappingFields.xml");
  9.  
  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];
  16.  
  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;
  26.  
  27.                         // Gets the lookup field instance
  28.                         var lookupValueList = (item[mappedField.Key] as SPFieldLookupValueCollection).ToList();
  29.  
  30.                         // Gets the taxonomy field instance
  31.                         TaxonomyField managedField = item[mappedField.Value] as TaxonomyField;
  32.  
  33.                         // Gets the current taxonomy session
  34.                         TaxonomySession session = new TaxonomySession(web.Site, false);
  35.  
  36.                         // Gets the term store (by SspId)
  37.                         var termStoreCol = session.TermStores[managedField.SspId];
  38.  
  39.                         // Gets the terms of a specific term set (by TermSetId)
  40.                         var termCol = termStoreCol.GetTermSet(managedField.TermSetId).Terms;
  41.  
  42.                         var listTerms = new List<Term>();
  43.  
  44.                         // Iterates through the lookup values
  45.                         foreach (var itemValue in lookupValueList)
  46.                         {
  47.                             string value = itemValue.LookupValue;
  48.  
  49.                             // Gets the correspondent term for the each value
  50.                             // found in the lookup values list
  51.                             var termToSet = termCol[value];
  52.  
  53.                             listTerms.Add(termToSet);
  54.                         }
  55.  
  56.                         // Sets the field value using the list of terms
  57.                         managedField.SetFieldValue(item, listTerms);
  58.  
  59.                         // Persists the item
  60.                         item.SystemUpdate();
  61.  
  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.

Reference:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx

Cheers,

Marcel Medina

Click here to read the same content in Portuguese.

blog comments powered by Disqus