Get name of Term from Managed metadata service by Guid
-
15-01-2021 - |
Question
function execOperation() {
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStores = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStore = termStores.getByName(att["data-termname"]);
$.each(att["data-termset"].split(','), function (i, x) {
var termSet = termStore.getTermSet(x); //Guid // 5719b9cf-8da0-4a03-bc33-f47c81fb696a
console.log(termSet.get_name()); // <- this one dont work ofc.
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(function () {
var termEnumerator = terms.getEnumerator();
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current().$2_0.$K_0;
var TermPath = currentTerm.PathOfTerm.split(';');
}
});
})
}
I'm using this code to access my Managed Meta data. The problem I can't seem to solve it how do I get the name of termSet
.
As you can see in the image below, I used the guid to load "Menu" from "Popermo Navigation", But when i run console.log(termSet)
I cant find the name anywhere.
Is there a way to get the name Aka "Menu"?
As you can see in the image below.
Solution
You need to explicitly load the termSet. JSOM only fetches the data that you request for.
Modify the code as below, will work:
var termSet = termStore.getTermSet(x); //Guid // 5719b9cf-8da0-4a03-bc33-f47c81fb696a
var terms = termSet.getAllTerms();
//load the term set here and request its data
context.load(termSet);
context.load(terms);
context.executeQueryAsync(function () {
// you will get the name here
// need to explicitly load the termset
console.log(termSet.get_name());
var termEnumerator = terms.getEnumerator();
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current();
// get data from term
var TermPath = currentTerm.get_pathOfTerm();
var TermName = currentTerm.get_name();
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange