1

I am getting below error on the second request. Strange thing is its not occurring on first request.

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.

function RequestSP(siteUrl, listTitle, xmlCamlQuery, viewFields) {
    debugger;
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle(listTitle);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(xmlCamlQuery);

    this.spListItemCollection = oList.getItems(camlQuery);

    var includeFields = JoinViewFieldsByComma(viewFields);
    console.log(includeFields);
    clientContext.load(spListItemCollection, "Include(" + includeFields + ")");

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    debugger;
    var listItemInfo = '';

    var listItemEnumerator = spListItemCollection.getEnumerator();//ERROR

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        for (var index in this.ViewFields) {
            var internalName = this.ViewFields[index][0];
            listItemInfo += "\n" + internalName + ": " + oListItem.get_item(internalName);
        }
    }

    console.log("Success...");
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

EDIT

So one thing i did not mention that I was loading SPRuntime with every request and I updated it with below which worked for me.

var spRuntimeLoaded = false;
if (spRuntimeLoaded) {
    RequestSP(siteUrl, listTitle, xmlCamlQuery, viewFields);
}
else {
    var scriptbase = siteUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js",
                    function () {
                        spRuntimeLoaded = true;
                        RequestSP(siteUrl, listTitle, xmlCamlQuery, viewFields);

                    }
            );
        });
}

1 Answer 1

0

Do you have any other code on the page that would be overwriting the spListItemCollection variable?

You can protect against global variable naming collisions by using an anonymous inline function in place of onQuerySucceeded, then using a locally-scoped variable for spListItemCollection (using var spListItemCollection = ... instead of this.spListItemCollection = ...).

function RequestSP(siteUrl, listTitle, xmlCamlQuery, viewFields) {
    debugger;
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(xmlCamlQuery);
    /* use a locally-scoped variable for spListItemCollection: */
    var spListItemCollection = oList.getItems(camlQuery);
    clientContext.load(spListItemCollection, "Include(" + includeFields + ")");
    clientContext.executeQueryAsync(Function.createDelegate(this, function(){
        debugger;
        var listItemInfo = '';
        var listItemEnumerator = spListItemCollection.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            for (var index in this.ViewFields) {
                var internalName = this.ViewFields[index][0];
                listItemInfo += "\n" + internalName + ": " + oListItem.get_item(internalName);
            }
        }
        console.log("Success...");
    }), Function.createDelegate(this, this.onQueryFailed));
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}
1
  • Thanks for the help but i found the issue. The issue was I was loading SPRuntime with every request so it was working for the first time and failed with second request. Commented Sep 17, 2015 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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