Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

JSOM: The collection has not been initialized.

I am using SharePoint 2010 and JSOM to get data from a list to populate a form that I have but I get the following error when I get to line 57.


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='VISN' /><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='VISN' /></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();

        $('#visn').val(oListItem.get_item('VISN'));
        $("#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('VISN'));
    }

}


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

Open in new window


Appreciate any help.

Thanks!
0
Isaac
Asked:
Isaac
  • 3
1 Solution
 
hieloCommented:
Try changing line 35 from:
this.collListItem = oList.getItems(camlQuery);

Open in new window

to
collListItem = oList.getItems(camlQuery);

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I still get the same error.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
There must be some type of conflict b/c when I comment out line 8, my code works.
0
 
leakim971PluritechnicianCommented:
you've asynchronous operation here and your object collListItem is global
I mean the second getListData don't wait the first to end so you run twice collListItem.getEnumerator() before a new init
(like the previous question about get_item)

here we create two collListItem, collListem1 and collListem2
// 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 clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle(listName);

    if (getQueryString('projNumber')) {
        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='VISN' /><FieldRef Name='Station_x0020_Number' /><FieldRef Name='Total_x0020_Project_x0020_Cost' /></ViewFields></View>";
    }
    else {
        var prjQry = "<View><Query></Query></View>";
    }

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

    this.collListItem1 = oList.getItems(camlQuery);

    clientContext.load(collListItem1);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pm_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
    );


    // Project Projections

    if (getQueryString('projNumber')) {
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='VISN' /></ViewFields></View>";
    }
    else {
        var prjQry = "<View><Query></Query></View>";
    }

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

    this.collListItem2 = oList.getItems(camlQuery);

    clientContext.load(collListItem2);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pp_OnQuerySucceeded),
        Function.createDelegate(this, this.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 pm_OnQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem1.getEnumerator();

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

        $('#visn').val(oListItem.get_item('VISN'));
        $("#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 = collListItem2.getEnumerator();

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

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

}


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

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now