I want to display the terms from a term store using Javascript Object Model. How can this be done?
5 Answers
I have blogged about it here: http://www.vrdmn.com/2012/12/working-with-taxonomy-and-javascript-in.html
The code:
$(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(){
//Current Context
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("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("b49f64b3-4722-4336-9a5c-56c326b344d4");
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(function(){
var termEnumerator = terms.getEnumerator();
var termList = "Terms: \n";
while(termEnumerator.moveNext()){
var currentTerm = termEnumerator.get_current();
termList += currentTerm.get_name() + "\n";
}
alert(termList);
},function(sender,args){
console.log(args.get_message());
});
}
I have added a input button with ID btnTaxonomies
, which is fetching the Terms from the Terms store using JSOM
$(document).ready(function () {
$('#btnTaxonomies').click(function () {
SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
$('#btnTaxonomies').val("Getting Taxonomies, please wait...");
SP.SOD.executeFunc('sp.taxonomy.js', false, Function.createDelegate(this, function () {
var context = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxonomySession.get_termStores().getByName("Managed Metadata Service");
var termSet = termStore.getTermSet("e51ca15d-a805-460e-ba5d-8f539b88cbdf");
var terms = termSet.getAllTerms();
context.load(terms);
context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
var termsEnumerator = terms.getEnumerator();
var menuItems = new Array();
$('#btnTaxonomies').val("Get the navigation Taxonomies");
while (termsEnumerator.moveNext()) {
var currentTerm = termsEnumerator.get_current();
document.write("<br>Name: " + currentTerm.get_name());
}
}), Function.createDelegate(this, function (sender, args) {
alert('The error has occured: ' + args.get_message());
}));
}));
});
I think there is already a similar question which has an answer for you. Query SP2013 Managed metadata term store
-
I tried that code. Specified argument was out of the range of valid values.Parameter name: index Getting the above error Commented Oct 1, 2013 at 11:18
I figured it out, don't know whether its perfect way out, for now it works...
Used TaxonomyHiddenList
to get the list of terms
var oListTax = context.get_web().get_lists().getByTitle('TaxonomyHiddenList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query></Query></View>');
collListItemTax = oListTax.getItems(camlQuery);
context.load(collListItemTax);
context.executeQueryAsync(function()
{
var listItemEnumerator = collListItemTax.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItemTax = listItemEnumerator.get_current();
alert(oListItemTax.get_item('Title'))
}
},
function()
{
});
-
3I would never advise to use the hidden list to do that. Commented Apr 20, 2016 at 12:27
Loading SharePoint TermStore is very easy. you have load default terms and then have to get the term store by name. and also you can get additional properties of the term store. you can get more details in this article. This Article may be helpful for you. Please mark it as answer if it is helpful.
http://www.sharepointsol.com/2015/06/SharePoint-2013-JSOM-Get-Term-Store.html