Share via


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

  • Anonymous
    June 17, 2015
    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.

  • Anonymous
    November 25, 2015
    The comment has been removed