Refer below JS file in your page where you need to bind the keywords:
- <script type="text/javascript" src="/_layouts/15/SP.Taxonomy.js"></script>
Use below Code to get Enterprise Keywords:
-
- var context = SP.ClientContext.get_current();
- var groupID;
- var groupName;
- var termSetID
- var termSetName;
- var currentTermName;
-
-
- $(document).ready(function () {
- getTermStores();
- });
-
-
- function getTermStores() {
- session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
- termStore = session.getDefaultSiteCollectionTermStore();
- context.load(session);
- context.load(termStore);
- context.executeQueryAsync(onListTaxonomySession, onFailListTaxonomySession);
- }
-
-
- function onListTaxonomySession () {
- groups = termStore.get_groups();
- context.load(groups);
- context.executeQueryAsync(onRetrieveGroups, onFailRetrieveGroups);
- }
-
-
- function onRetrieveGroups() {
- var groupEnum = groups.getEnumerator();
- while (groupEnum.moveNext()) {
- var currentGroup = groupEnum.get_current();
- groupID = currentGroup.get_id();
- groupName = currentGroup.get_name();
- if (groupName == "System") {
- showTermSets(groupID,groupName);
- }
- }
- }
-
-
- function showTermSets(groupID, groupName) {
- var groupEnum = groups.getEnumerator();
- while (groupEnum.moveNext()) {
- var currentGroup = groupEnum.get_current();
- if (currentGroup.get_id() == groupID) {
-
-
- context.load(currentGroup);
- context.executeQueryAsync(
- function () {
-
-
-
-
-
-
- var termSets = currentGroup.get_termSets();
- context.load(termSets);
- context.executeQueryAsync(
- function () {
- var termSetEnum = termSets.getEnumerator();
- while (termSetEnum.moveNext()) {
- var currentTermSet = termSetEnum.get_current();
- termSetName = currentTermSet.get_name();
- termSetID = currentTermSet.get_id();
- if (termSetName == "Keywords") {
- showTerms(groupID, groupName,termSetID,termSetName);
- }
- }
- },
- function () {
-
- alert('An error occurred in loading the term sets for this group:' + args.get_message());
- });
- },
- function () {
-
- alert('An error occurred in loading the term sets for this group: ' + args.get_message());
- });
- break;
- }
- }
- }
-
-
- function showTerms(groupID, groupName, termSetID, termSetName) {
- var groupEnum = groups.getEnumerator();
- while (groupEnum.moveNext()) {
- var currentGroup = groupEnum.get_current();
- if (currentGroup.get_name() == groupName) {
- context.load(currentGroup);
- context.executeQueryAsync(
-
-
-
-
-
- function () {
- var termSets = currentGroup.get_termSets();
- context.load(termSets);
- context.executeQueryAsync(
- function () {
-
-
-
-
-
- var termSetEnum = termSets.getEnumerator();
- while (termSetEnum.moveNext()) {
- var currentTermSet = termSetEnum.get_current();
- if (currentTermSet.get_name() == termSetName) {
-
-
- context.load(currentTermSet);
- context.executeQueryAsync(
- function () {
-
-
-
-
-
-
- var terms = currentTermSet.get_terms();
- context.load(terms);
- context.executeQueryAsync(
- function () {
-
-
-
- var termsEnum = terms.getEnumerator();
- while (termsEnum.moveNext()) {
- var currentTerm = termsEnum.get_current();
- currentTermName = currentTerm.get_name();
-
- $('#NewsCategory').append('<option value="' + currentTermName + '">' + currentTermName + '</option>');
- }
- },
- function () {
-
- alert('An error occurred when trying to retrieve terms in this term set:'+args.get_message());
- });
- },
- function () {
- alert('An error occurred when trying to retrieve terms in this term set:'+args.get_message());
-
- });
- break;
- }
- }
- },
- function () {
-
- alert('An error occurred when trying to retrieve terms in this term set:' + args.get_message());
- });
- },
- function () {
-
- alert('An error occurred when trying to retrieve terms in this term set:' + args.get_message());
- });
- break;
- }
- }
- }
-
- function onFailListTaxonomySession(sender, args) {
- alert('An Error Occured when trying to retrive Taxonomy session:' + args.get_message());
- }
-
- function onFailRetrieveGroups(sender, args) {
- alert("Failed to retrieve groups. Error:" + args.get_message());
- }
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