Retrieve Enterprise Keywords from Taxonomy in SharePoint Hosted APP

Refer below JS file in your page where you need to bind the keywords:

  1. <script type="text/javascript" src="/_layouts/15/SP.Taxonomy.js"></script>  
Use below Code to get Enterprise Keywords:
  1. //Current Context  
  2. var context = SP.ClientContext.get_current();  
  3. var groupID;  
  4. var groupName;  
  5. var termSetID  
  6. var termSetName;  
  7. var currentTermName;  
  8.   
  9. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  10. $(document).ready(function () {  
  11.     getTermStores();  
  12. });  
  13.   
  14. //Get Term Store from Taxonomy  
  15. function getTermStores() {  
  16.     session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
  17.     termStore = session.getDefaultSiteCollectionTermStore();  
  18.     context.load(session);  
  19.     context.load(termStore);  
  20.     context.executeQueryAsync(onListTaxonomySession, onFailListTaxonomySession);  
  21. }  
  22.   
  23. //Load All Groups from Term Store  
  24. function onListTaxonomySession () {  
  25.     groups = termStore.get_groups();  
  26.     context.load(groups);  
  27.     context.executeQueryAsync(onRetrieveGroups, onFailRetrieveGroups);  
  28. }  
  29.   
  30. //Get Only Sytem Group  
  31. function onRetrieveGroups() {  
  32.     var groupEnum = groups.getEnumerator();  
  33.     while (groupEnum.moveNext()) {  
  34.         var currentGroup = groupEnum.get_current();  
  35.         groupID = currentGroup.get_id();  
  36.         groupName = currentGroup.get_name();  
  37.         if (groupName == "System") {  
  38.             showTermSets(groupID,groupName);  
  39.         }  
  40.     }  
  41. }  
  42.   
  43. //Get only keywords Group from System Group  
  44. function showTermSets(groupID, groupName) {  
  45.     var groupEnum = groups.getEnumerator();  
  46.     while (groupEnum.moveNext()) {  
  47.         var currentGroup = groupEnum.get_current();  
  48.         if (currentGroup.get_id() == groupID) {  
  49.             // We need to load and populate the matching group first, or the  
  50.             // term sets that it contains will be inaccessible to our code.  
  51.             context.load(currentGroup);  
  52.             context.executeQueryAsync(  
  53.             function () {  
  54.                 // debugger;  
  55.                 // The group is now available becuase this is the  
  56.                 // success callback. So now we'll load and populate the  
  57.                 // term set collection. We have to do this before we can  
  58.                 // iterate through the collection, so we can do this  
  59.                 // with the following nested executeQueryAsync method call.  
  60.                 var termSets = currentGroup.get_termSets();  
  61.                 context.load(termSets);  
  62.                 context.executeQueryAsync(  
  63.                 function () {  
  64.                     var termSetEnum = termSets.getEnumerator();  
  65.                     while (termSetEnum.moveNext()) {  
  66.                         var currentTermSet = termSetEnum.get_current();  
  67.                         termSetName = currentTermSet.get_name();  
  68.                         termSetID = currentTermSet.get_id();  
  69.                         if (termSetName == "Keywords") {  
  70.                             showTerms(groupID, groupName,termSetID,termSetName);  
  71.                         }  
  72.                     }  
  73.                 },  
  74.                 function () {  
  75.                     //Failure to load term set  
  76.                     alert('An error occurred in loading the term sets for this group:' + args.get_message());  
  77.                 });  
  78.             },  
  79.             function () {  
  80.                 //Failure to load current group  
  81.                 alert('An error occurred in loading the term sets for this group: ' + args.get_message());  
  82.             });  
  83.             break;  
  84.         }  
  85.     }  
  86. }  
  87.   
  88. //Get all Keywords  
  89. function showTerms(groupID, groupName, termSetID, termSetName) {  
  90.     var groupEnum = groups.getEnumerator();  
  91.     while (groupEnum.moveNext()) {  
  92.         var currentGroup = groupEnum.get_current();  
  93.         if (currentGroup.get_name() == groupName) {  
  94.             context.load(currentGroup);  
  95.             context.executeQueryAsync(  
  96.             // The group is now available becuase this is the  
  97.             // success callback. So now we'll load and populate the  
  98.             // term set collection. We have to do this before we can  
  99.             // iterate through the collection, so we can do this  
  100.             // with the following nested executeQueryAsync method call.  
  101.             function () {  
  102.                 var termSets = currentGroup.get_termSets();  
  103.                 context.load(termSets);  
  104.                 context.executeQueryAsync(  
  105.                 function () {  
  106.                     // debugger;  
  107.                     // The term sets are now available because this is the  
  108.                     // success callback. So now we'll iterate through the collection  
  109.                     // and get a reference to the specific term set that was represented  
  110.                     // by the clicked div.  
  111.                     var termSetEnum = termSets.getEnumerator();  
  112.                     while (termSetEnum.moveNext()) {  
  113.                         var currentTermSet = termSetEnum.get_current();  
  114.                         if (currentTermSet.get_name() == termSetName) {  
  115.                             // We need to load and populate the term set, so that we can  
  116.                             // access the terms in it.  
  117.                             context.load(currentTermSet);  
  118.                             context.executeQueryAsync(  
  119.                             function () {  
  120.                                 // debugger;  
  121.                                 // Now we have access to the term set because this is the  
  122.                                 // success callback, so we can now create and populate a collection  
  123.                                 // object to hold the actual terms.  
  124.                                 // Note that we need to do one final load and populate before we  
  125.                                 // can iterate over the collection object.  
  126.                                 var terms = currentTermSet.get_terms();  
  127.                                 context.load(terms);  
  128.                                 context.executeQueryAsync(  
  129.                                 function () {  
  130.                                     //debugger;  
  131.                                     // Now we can iterate over the terms because this is the  
  132.                                     // success callback. So we'll build an indented list of terms  
  133.                                     var termsEnum = terms.getEnumerator();  
  134.                                     while (termsEnum.moveNext()) {  
  135.                                         var currentTerm = termsEnum.get_current();  
  136.                                         currentTermName = currentTerm.get_name();  
  137.                                         // '#NewsCategory' is ID of HTML Select  
  138.                                         $('#NewsCategory').append('<option value="' + currentTermName + '">' + currentTermName + '</option>');  
  139.                                     }  
  140.                             },  
  141.                             function () {  
  142.                                 //Failure to load terms  
  143.                                 alert('An error occurred when trying to retrieve terms in this term set:'+args.get_message());  
  144.                             });  
  145.                         },  
  146.                         function () {  
  147.                             alert('An error occurred when trying to retrieve terms in this term set:'+args.get_message());  
  148.                             //Failure to load the current term set  
  149.                         });  
  150.                         break;  
  151.                     }  
  152.                 }  
  153.             },  
  154.             function () {  
  155.                 //Failure to load term sets  
  156.                 alert('An error occurred when trying to retrieve terms in this term set:' + args.get_message());  
  157.             });  
  158.         },  
  159.         function () {  
  160.             //Failure to load current group  
  161.             alert('An error occurred when trying to retrieve terms in this term set:' + args.get_message());  
  162.         });  
  163.         break;  
  164.         }  
  165.     }  
  166. }  
  167.   
  168. function onFailListTaxonomySession(sender, args) {  
  169.     alert('An Error Occured when trying to retrive Taxonomy session:' + args.get_message());  
  170. }  
  171.   
  172. function onFailRetrieveGroups(sender, args) {  
  173.     alert("Failed to retrieve groups. Error:" + args.get_message());  
  174. }  

 

Note:

Give Read permission to Taxonomy in APPManifeast.xml as shown below:

Summary

In this code snippet we have explored how to get all keywords from Taxonomy using JavaScript. I hope that the above code will be very useful to you and also I’ve attached the solution for your more reference.

Happy Coding