0

Can anyone help on this.

I am getting the below error.

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

following is 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");
            // SP.SOD.executeOrDelayUntilScriptLoaded('SP.taxonomy.js');
            ExecuteOrDelayUntilScriptLoaded(execOperation, "sp.taxonomy.js");
        });
    });
});


function execOperation(){

    var context = new SP.ClientContext.get_current();    
    console.log(context);
    //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("Managed Metadata Service");
    //GUID of Term Set from which to get the Terms.
    var termSet = termStore.getTermSet("8a675225-852f-48ed-af73-8fd8e5ccdc14");
    var terms = termSet.getAllTerms();
    context.load(terms);
    termEnumerator = terms.getEnumerator();

    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());
    });
}
  • please use a debugger and see where the error occurs (which line?) and update your question. – helb Jun 6 '18 at 8:05
1

Comment the line

termEnumerator = terms.getEnumerator();

before you are calling the context.executeQueryAsync() getEnumerator will not be initialized before the executeQueryAsync function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.