How to get termStore by name using SharePoint 2013 JSOM

All the examples of retrieving Terms Store and getting all terms using JavaScript object model were using GUIDs. Well, we know that GUIDs work only for specific environment, and not for moving stuff around.

You will need to include sp.js and SP.Taxonomy.js. The way you do it will differ based on whether you are running inside SharePoint web (example) or inside app (check Taxonomy Menu in  Office App Samples).

So once you are at the point when JS libraries are loaded, what do you need:

var termSetName = "Your term set name";
var locale = 1033; // your locale. Here is English

var clientContext  = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
var termSets = termStore.getTermSetsByName(termSetName, locale);
var termSet = termSets.getByName(termSetName);
var terms = termSet.getAllTerms();

clientContext.load(taxonomySession);
clientContext.load(termStore);
clientContext.load(termSet);
clientContext.load(terms);

clientContext.executeQueryAsync(function onSuccess() {
    var enumerator = terms.getEnumerator();

    while (enumerator.moveNext()) {
        var spTerm = enumerator.get_current();
        var name = spTerm.get_name();
        var id = spTerm.get_id();
        // ... etc, do your logic for terms
    }
}, function onFailure(args) {
    alert('Error: '+args.get_message());
});
This entry was posted in Sharepoint, SharePoint 2013. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *