I am trying to get the Label(Title - shown in the Navigation UI) of a term through JSOM. I have successfully got the ID of the term.
Now I got stuck in getting the Label of term using ID. I have pasted my code below. Any suggestions, what am I missing here?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(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", execOperation);
});
}
);
});
function execOperation(){
var termSetName = "ADLA";
var locale = 1033;
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 chaparray={};
var enumerator = terms.getEnumerator();
while (enumerator.moveNext()) {
var spTerm = enumerator.get_current();
var name = spTerm.get_name();
var id = spTerm.get_id();
}
}, function onFailure(args) {
alert('Error: '+args.get_message());
});
}
</script>
Updated:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
var taxonomySession;
var clientContext;
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function(){
$.getScript(scriptbase + "SP.Taxonomy.js", execOperation);
});
}
);
});
function execOperation(){
var termSetName = "ADLA";
var locale = 1033;
clientContext = SP.ClientContext.get_current();
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 chaparray={};
var enumerator = terms.getEnumerator();
while (enumerator.moveNext()) {
var spTerm = enumerator.get_current();
var name = spTerm.get_name();
var id = spTerm.get_id();
getTermDefaultValue(id,1033);
}
},
function onFailure(args) {
alert('Error: '+args.get_message());
});
}
function getTermDefaultValue(termId, lcid) {
var termDefaultValue = taxonomySession.getTerm(termId).getDefaultLabel(lcid);
}
</script>
Below is the image.. I need to get the "Chapter-1" text.
var taxonomySession = new TaxonomySession(site);
var termStore = taxonomySession.DefaultKeywordsTermStore;
var termStoreGroup = termStore.GetSiteCollectionGroup(site);
var termSet = termStoreGroup.CreateTermSet("ADLA");
termSet.IsAvailableForTagging = true;
termSet.TermStore.CommitAll();