JavaScript client library and taxonomy fields

With the JavaScript API it’s possible to retrieve information about all the fields in a list. In most cases this will work without a hitch, but the problem is when you are using TaxonomyFields in your list.

Normally you will receive an array of objects containing methods such as get_internalName(), get_typeAsString() etc. If you have taxonomy fields in your list, the entry in the array doesn’t contain the expected object, but rather just the raw data.

After a lot of investigating and troubleshooting, I discovered that the TaxonomyField type isn’t defined together with all the other field types. It’s defined in SP.Taxonomy.js; this is part of SharePoint Server, not SharePoint Foundation.

If you include a reference to SP.Taxonomy.js and make sure it has been loaded, everything works as expected.

Advertisements

2 Responses to JavaScript client library and taxonomy fields

  1. Do you have an example of how to access a TermSet? I’ve been trying a few things with no luck. The following errors on context.load(taxSession).

    function areMMSFieldsCreated() {
    this.site = context.get_site();
    this.taxSession = new SP.Taxonomy.TaxonomySession(context, site, false);
    this.termStores = taxSession.get_termStores();

    context.load(site);
    context.load(taxSession);
    context.load(termStores);
    //context.load(termSet);
    context.executeQueryAsync(Function.createDelegate(this, this.areMMSFieldsCreatedPassed), Function.createDelegate(this, this.areMMSFieldsCreatedFailed));

    }

    function areMMSFieldsCreatedPassed(sender, args){
    this.termSet = taxSession.getTermSet(“Department”);

    alert(“Term Stores: ” + termStores.get_count());
    alert(“Term Sets (Department): ” + termSet.get_count);
    }

    • Sorry, but I haven’t tried doing anything with Term Sets

      I had a quick look at the SP.Taxonomy.js file and my guess is that the problem is related in how you create the taxonomy session. You create it using ‘new’; this seems a bit off compared to the rest of the javascript patterns they use.

      There is a SP.Taxonomy.TaxonomySession.getTaxonomySession method that sounds promising. This takes a parameter ‘a’ that looks like should be the context object.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: