-1

I am enumerating all terms using SP.Taxonomy.js JSOM in SharePoint.While enumerating I want to check if currentTerm has children or not.I need some property to check like children count.How can I do this with minimum round trip to server. I am using following code get taxonomy and it is working fine.

Please help

 $(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
    SP.SOD.registerSod('sp.taxonomy.js', "/_layouts/15/sp.taxonomy.js");
`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("Taxonomy_qeFlDdEX32yZ3Q7EpEIeMQ==");
    var termSet = termStore.getTermSet("ed6d3beb-6a49-4444-bc5d-456f747e139d");
    var terms = termSet.getAllTerms();
    context.load(terms);
    context.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
    var termsEnumerator = terms.getEnumerator();
    var menuItems = new Array();
    while (termsEnumerator.moveNext()) {
    var currentTerm = termsEnumerator.get_current();
    var targetGroups = document.getElementById("selectTaxonomy");
    var taxoGroup = document.createElement("option");
    taxoGroup.text = currentTerm.get_name();
    targetGroups.add(taxoGroup);
    }
    }), Function.createDelegate(this, function (sender, args) {
    alert('The error has occured: ' + args.get_message());
    }));
    }));
    },"sp.js")
    });
A_____
  • 364
  • 3
  • 19
  • What is the question? You're only executing a request once, so why aren't you happy with your code? – Daniel B Apr 13 '15 at 20:51

1 Answers1

0

You could use SP.Taxonomy.Term.termsCount property to get the number of child Term objects, for example:

var termSetId = '--guid goes here--';
getTerms(termSetId, 
 function(terms){
    for(var i = 0;i < terms.get_count();i++){
        var term = terms.get_item(i);
        var hasChildTerms =  (term.get_termsCount() > 0);
        //...
    }
 },
 function(sender,args) 
 {
    console.log(args.get_message());
 });

where

function getTerms(termSetId,success,error) {
   var context = SP.ClientContext.get_current();
   var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
   var termStore = taxSession.getDefaultSiteCollectionTermStore();
   var termSet = termStore.getTermSet(termSetId);
   var terms = termSet.getAllTerms();
   context.load(terms);
   context.executeQueryAsync(function () {
        success(terms)
   },
   error);
}

Some recommendations

1) Prefer SP.SOD.loadMultiple function for loading multiple libraries, for example

SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
SP.SOD.registerSod('SP.Taxonomy.TaxonomySession', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['SP.ClientContext', 'SP.Taxonomy.TaxonomySession'], function(){

     //your code goes here

});     

2) Avoid any hard-coding, for example:

/_layouts/15/sp.taxonomy.js -> SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js')

Replace the line:

var termStore = taxonomySession.get_termStores().getByName("Taxonomy_qeFlDdEX32yZ3Q7EpEIeMQ==");

with this one:

var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
  1. Function.createDelegate Function could be avoided in most cases
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193