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:
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
- static void Main(string[] args)
- {
- var url = ConfigurationManager.AppSettings["Url"].ToString();
- var library = ConfigurationManager.AppSettings["Library"].ToString();
-
- // Reads XML, considering
- // (key => lookup field,value => taxonomy field)
- Dictionary<string, string> mappingFields = XMLHelper.ReadDictionaryXML("MappingFields.xml");
-
- using (SPSite site = new SPSite(url))
- {
- using (SPWeb web = site.RootWeb)
- {
- // Gets the list/library
- SPList list = web.Lists[library];
-
- foreach (SPListItem item in list.Items)
- {
- // Iterates through all mapped fields
- foreach (var mappedField in mappingFields)
- {
- if (item.Fields.ContainsField(mappedField.Key))
- {
- // Allows updates without a trace
- web.Site.AllowUnsafeUpdates = true;
-
- // Gets the lookup field instance
- var lookupValueList = (item[mappedField.Key] as SPFieldLookupValueCollection).ToList();
-
- // Gets the taxonomy field instance
- TaxonomyField managedField = item[mappedField.Value] as TaxonomyField;
-
- // Gets the current taxonomy session
- TaxonomySession session = new TaxonomySession(web.Site, false);
-
- // Gets the term store (by SspId)
- var termStoreCol = session.TermStores[managedField.SspId];
-
- // Gets the terms of a specific term set (by TermSetId)
- var termCol = termStoreCol.GetTermSet(managedField.TermSetId).Terms;
-
- var listTerms = new List<Term>();
-
- // Iterates through the lookup values
- foreach (var itemValue in lookupValueList)
- {
- string value = itemValue.LookupValue;
-
- // Gets the correspondent term for the each value
- // found in the lookup values list
- var termToSet = termCol[value];
-
- listTerms.Add(termToSet);
- }
-
- // Sets the field value using the list of terms
- managedField.SetFieldValue(item, listTerms);
-
- // Persists the item
- item.SystemUpdate();
-
- // Denies further unsafe updates
- web.Site.AllowUnsafeUpdates = false;
- }
- }
- }
- }
- }
- }
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.