How to query SP2013 Managed metadata term store with JSOM

This post will show how to read the Taxonomy Term Store with JSOM.
Taxonomy Term store can be accessed in a SharePoint App, with permission scope of Taxonomy.
To be able to use the JSOM API for Taxonomy you need to reference SP.Taxonomy.js found in _layouts/15/ the best way to reference this would be in the document ready.

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

Read from Taxonomy

 function getTermStores() {  
   session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
   termStores = session.get_termStores();  
   context.load(session);  
   context.load(termStores);  
   context.executeQueryAsync(  
 function(){  
 termStoreEnum = termStores.getEnumerator();  
    var termStores = "TermStores: /n";  
    while (termStoreEnum.moveNext()) {  
     var currentTermStore = termStoreEnum.get_current();  
     var termStoreID = currentTermStore.get_id();  
     var termStoreName = currentTermStore.get_name();  
 termStores += "Name: " + termStoreName + " ID:" + termStoreID;  
 }  
 }, function(){  
 //failure loading.  
 });  
 }  

Comments

Popular posts from this blog

Search Query by ContentType name issue

Search Result Webpart,Filter result If QueryString is present in URL

SharePoint 2013 Workflow gets canceled automatically