getDefaultSiteCollectionTermStore() – JavaScript runtime error: Unable to get property ‘toString’ of undefined or null reference

Today I was venturing in the world of JavaScript and SharePoint 2013 Hosted Apps. I was trying to perform a simple JavaScript that access the Managed Term Store to bring back results.

Take a look at my code below.

var context;

var termStore;

var groups;

$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
  $.getScript(scriptbase + "SP.Runtime.js",   function () {
      $.getScript(scriptbase + "SP.js", function () {
        $.getScript(scriptbase + "SP.Taxonomy.js", function () {
              context = SP.ClientContext.get_current();
              //Call your code here.

function defaultTermStore() {
session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStore = session.getDefaultSiteCollectionTermStore();
context.executeQueryAsync(successDefaultTermStore, failedListTaxonomySession);

function successDefaultTermStore() {
groups = termStore.get_groups();
function () {
var groupList = "List: \n";
var groupsEnum = groups.getEnumerator();
while (groupsEnum.moveNext()) {
var currentGroup = groupsEnum.get_current();
var groupName = currentGroup.get_name();
groupList += groupName + "\n";
}, function () {

If you review the code above, this code loads the SP.Taxonomy.js file, then loads the TaxonomySession from context, calls session.getDefaultSiteCollectionTermStore(). Loads that term store, then gets all groups belonging to that term store, then lastly prints them out to an alert window.

Every time I ran this code I was getting the error message.

The reason why I was getting this message was because the properties of the Managed Metadata Service Connection didn’t have “This service application is the default storage location for column specific term sets” ticked.

You need to go into Central administration > Application Management > Manage service applications. Find your Managed Metadata Service, then select the Managed Metadata Service Connection, and click Properties on the ribbon.

Then tick ensure the second option is ticked.

If you re-run the code you should find it now works.