0

I get the following error in SharePoint 2010 using JSOM and not sure why. The error occurs when I get to "pp_OnQuerySucceeded()".

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.

 // JavaScript source code$(document).ready(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
});

function retrieveData() {
getListData("Project Module", "pm_OnQuerySucceeded", "queryError");
getListData("Project Projections", "pp_OnQuerySucceeded", "queryError");
}


var getQueryString = function (field, url) {
var href = url ? url : window.location.href;
var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
var string = reg.exec(href);
return string ? string[1] : null;
};

function getListData(listName) {
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getByTitle(listName);

if (getQueryString('projNumber')) {
    if (listName == "Project Module")
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Project_x0020_Number' /><FieldRef Name='Station_x0020_Name' /><FieldRef Name='BSN' /><FieldRef Name='Station_x0020_Number' /><FieldRef Name='Total_x0020_Project_x0020_Cost' /></ViewFields></View>";
    else
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='BSN' /></ViewFields></View>";
}
else {
    var prjQry = "<View><Query></Query></View>";
}

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(prjQry);
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

if (listName == "Project Module") {
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pm_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
        );
}
else {
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pp_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
        );

}

}

function pm_OnQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();

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

    $('#bsn').val(oListItem.get_item('BSN'));
    $("#projTitle").val(oListItem.get_item('Title'));
    $('#station').val(oListItem.get_item('Station_x0020_Number'));
    $('#stationName').val(oListItem.get_item('Station_x0020_Name'));
    $('#projNumber').val(oListItem.get_item('Project_x0020_Number'));
    $('#totProjCost').val(oListItem.get_item('Total_x0020_Project_x0020_Cost'));
}

}

function pp_OnQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();

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

    $('#designPlan').val(oListItem.get_item('BSN'));
}

}


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

}

I notice that if I comment out the following line, "getListData("Project Projections", "pp_OnQuerySucceeded", "queryError");", it works.

Any help would be appreciated. Thanks!

  • It is because you're using .executeQueryAsync(). So both of your calls are executed asynchronously. Thsi means only one ListItemCollection gets loaded (clientContext.load(collListItem);), and the other "has not been initialized" – Gintas K Nov 22 '16 at 16:22
  • I f you need multiple asynchronous calls, have a look here, or you can use REST API instead. – Gintas K Nov 22 '16 at 16:24
1

Can you try below code ? Idea is to create 2 different collection variable for CollectionListItem. I have marked changes with comment...

    // JavaScript source code$(document).ready(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
});

function retrieveData() {
getListData("Project Module", "pm_OnQuerySucceeded", "queryError");
getListData("Project Projections", "pp_OnQuerySucceeded", "queryError");
}


var getQueryString = function (field, url) {
var href = url ? url : window.location.href;
var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
var string = reg.exec(href);
return string ? string[1] : null;
};

function getListData(listName) {
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getByTitle(listName);

if (getQueryString('projNumber')) {
    if (listName == "Project Module")
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Project_x0020_Number' /><FieldRef Name='Station_x0020_Name' /><FieldRef Name='BSN' /><FieldRef Name='Station_x0020_Number' /><FieldRef Name='Total_x0020_Project_x0020_Cost' /></ViewFields></View>";
    else
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='BSN' /></ViewFields></View>";
}
else {
    var prjQry = "<View><Query></Query></View>";
}

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


//this.collListItem = oList.getItems(camlQuery); // COMMENTED THIS LINE
//  clientContext.load(collListItem); // COMMENTED THIS LINE

if (listName == "Project Module") {
    this.collListItemPM = oList.getItems(camlQuery); // // ADDED THIS LINE
    clientContext.load(collListItemPM); // // ADDED THIS LINE
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pm_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
        );
}
else {
    this.collListItemPP = oList.getItems(camlQuery); // // ADDED THIS LINE
    clientContext.load(collListItemPP); // // ADDED THIS LINE
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pp_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
        );

}

}

function pm_OnQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItemPM.getEnumerator(); // CHANGED VARIABLE NAME collListItem TO collListItemPM

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

    $('#bsn').val(oListItem.get_item('BSN'));
    $("#projTitle").val(oListItem.get_item('Title'));
    $('#station').val(oListItem.get_item('Station_x0020_Number'));
    $('#stationName').val(oListItem.get_item('Station_x0020_Name'));
    $('#projNumber').val(oListItem.get_item('Project_x0020_Number'));
    $('#totProjCost').val(oListItem.get_item('Total_x0020_Project_x0020_Cost'));
}

}

function pp_OnQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItemPP.getEnumerator(); // CHANGED VARIABLE NAME collListItem TO collListItemPP

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

    $('#designPlan').val(oListItem.get_item('BSN'));
}

}


function queryError(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
  • Ok. That worked. I have to do three other different query calls. I will use the same method. Do you think this is the best approach? Thanks! – JustMe Nov 22 '16 at 18:09
  • yes..you have to create separate collection objects because of async nature of calls. this collection object will be loaded only once response is retrieved from server and hence we need this trick.... :) – Siddharth Vaghasia Nov 22 '16 at 18:12

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.