0

I'm trying to do something similar to this, but with a dynamic calling function name so that I can reuse the function. I followed Danny's suggestion, but I am getting the error:

Uncaught TypeError: Cannot read property 'getEnumerator' of undefined

With the below code, I'm hoping someone might be able to help me figure out why it won't load the data. I did get everything to work up to and including the output of "Entered loadBudgetYear".

    // Load the required SharePoint libraries.
$(document).ready(function () {

    hostweburl="https://mysite.sharepoint.com";

    // The js files are in a URL in the form:
    // web_url/_layouts/15/resource_file
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js files and continue to
    // the execOperation function.
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", loadReady);
        }
    );
});

function loadReady() {

    var url = 'https://mysite.sharepoint.com';

    retrieveListItems(url, loadBudgetYear(), "listBudget");
    retrieveListItems(url, loadDropdown2(), "listDropdown2");
    retrieveListItems(url, loadDropdown3(), "listDropdown3");

};

// Container for retrieving list items from SharePoint site
function retrieveListItems(siteUrl, cFunction, listName) {
    var request={};
    request.listName = listName;
    request.siteUrl = siteUrl || _spPageContextInfo.siteAbsoluteUrl;

    var clientContext = new SP.ClientContext(request.siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle(request.listName);

    request.camlQuery = new SP.CamlQuery();

    request.collListItem = oList.getItems(request.camlQuery);

    clientContext.load(request.collListItem);

    clientContext.executeQueryAsync(
        Function.createDelegate(cFunction.bind(request)),
        Function.createDelegate(this, this.onQueryFailed)
    );

}

function onQueryFailed(sender, args) {

    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function loadBudgetYear() {
    console.log("Entered loadBudgetYear");

    var received=this;
    received.items=[];
    var listItemInfo = '';
    var listItemEnumerator = received.collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();        
        listItemInfo += '<option value =' + oListItem.get_item('BudgetYearID') + '>' + oListItem.get_item('BudgetYear') + '</option>';
    }

    $('#budgetYr').append(listItemInfo.toString());
};

function loadDropdown2() {

    var received=this;
    received.items=[];
    var listItemInfo = '';
    var listItemEnumerator = received.collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();        
        listItemInfo += '<option value =' + oListItem.get_item('fooID') + '>' + oListItem.get_item('foo') + '</option>';
    }

    $('#dropdown2').append(listItemInfo.toString());
};

function loadDropdown3() {

    var received=this;
    received.items=[];
    var listItemInfo = '';
    var listItemEnumerator = received.collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();        
        listItemInfo += '<option value =' + oListItem.get_item('barID') + '>' + oListItem.get_item('bar') + '</option>';
    }

    $('#dropdown3').append(listItemInfo.toString());
};
0

In your function ready you should pass a reference to your next function (which get executed when a request is ready). In your code you execute the function instead of passing the reference. Consider how the code should look like:

function loadReady() {

    var url = 'https://mysite.sharepoint.com';

    retrieveListItems(url, loadBudgetYear, "listBudget");
    retrieveListItems(url, loadDropdown2, "listDropdown2");
    retrieveListItems(url, loadDropdown3, "listDropdown3");

};  

You should also fix your retrieveListItems function:

clientContext.executeQueryAsync(
        Function.createDelegate(request, cFunction.bind(request)),
        Function.createDelegate(this, this.onQueryFailed)
    );
  • But these functions are getting executed. it is throwing an error while getting Enumerator on items. – Ganesh Sanap Nov 15 '18 at 7:08
  • The error is thrown because they executed too early. – Sergei Sergeev Nov 15 '18 at 7:12
  • I originally had passed the function itself instead of the reference because I was getting error: Uncaught TypeError: Cannot read property 'apply' of undefined at Array.<anonymous> (MicrosoftAjax.js:5) at MicrosoftAjax.js:5 at SP.ClientRequest.$3K_0 (<anonymous>:2:52181) at Array.<anonymous> (MicrosoftAjax.js:5) at MicrosoftAjax.js:5 at Sys.Net.WebRequest.completed (MicrosoftAjax.js:5) at XMLHttpRequest._onReadyStateChange (MicrosoftAjax.js:5) The script is loaded with: <script src="ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></…> – BWieland86 Nov 15 '18 at 13:22
  • Passing the function itself instead of the reference to the function allowed me to get around this error and load the function, but now I'm having issues with the getEnumerator(). – BWieland86 Nov 15 '18 at 13:26
  • please see my update – Sergei Sergeev Nov 15 '18 at 13:47

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.