Taxonomy fields return as Dictionaries using the Client Objcet Model in SharePoint 2013


Recently, a collegue of mine and I ran into this issue.

The Problem:

Let’s assume you execute the following code against one of your SharePoint lists that contains taxonomy fields

List oList = cc.Web.Lists.GetByTitle(listName);
CamlQuery camlQuery = newCamlQuery();
camlQuery.ViewXml = caml;
ListItemCollection collListItem = oList.GetItems(camlQuery);
cc.Load(collListItem);
cc.ExecuteQuery();

Firstly, we’ll assume (and you must),  you’ve added a reference to Microsoft.SharePoint.Client.Taxonomy.  Without a reference to the taxonomy assembly SharePoint has no idea what to do with the taxonomy fields in collListItem.

But yet, you look at what is returned and notice that taxonomy fields are returning as dictionaries while all other fields return correctly.

What’s going on here?

Looking at the data you can see that the dictionary for the taxonomy fields contains 2 fields

[0]: {[_ObjectType_, SP.Taxonomy.TaxonomyFieldValueCollection]}
[1]: {[_Child_Items_, System.Object[]]}

The first one, _ObjectType_, dictates what class SharePoint will use to house the data.

But wait, we referenced the taxonomy assembly in our project, why is this still returning as a dictionary?

The problem is that just because you reference an assembly does not automatically mean that the assembly will be loaded, assemblies may load when first used in your code.
When the ListItemCollection above is populated, Microsoft.SharePoint.Client.Taxonomy has not yet been loaded, the code checks to see if the SP.Taxonomy.TaxonomyFieldValueCollection class is available and if not you get a dictionary.

The Solution

You must force the Microsoft.SharePoint.Client.Taxonomy assembly to be loaded.
There are other ways to accomplish this but here is what we did.
Just before we execute the code to get our list data, we added this line.

//*** dummy to force Microsoft.SharePoint.Client.Taxonomy.dll to be loaded
TaxonomyItem dummy = new TaxonomyItem(clientContext,null);

That did the trick.  We also tried creating the TaxonomyItem as null thinking that might do it but it still failed, it had to be an actual instantiation.

The final code

//*** dummy to force Microsoft.SharePoint.Client.Taxonomy.dll to be loaded
TaxonomyItem dummy = new TaxonomyItem(clientContext,null);
List oList = cc.Web.Lists.GetByTitle(listName);
CamlQuery camlQuery = newCamlQuery();
camlQuery.ViewXml = caml;
ListItemCollection collListItem = oList.GetItems(camlQuery);
cc.Load(collListItem);
cc.ExecuteQuery();

The result

Comments (2)

  1. Andrew says:

    Thanks that's great, Thought I was going mad, Our code was failing on the first request to it but upon subsequent requests it would work. Random lol. thanks again.

  2. Derek Gusoff says:

    This is crazy and brilliant, and it worked for me. Typical SharePoint. Thank you!