Solved

JSOM: The collection has not been initialized.

Posted on 2016-11-21
5
22 Views
Last Modified: 2016-12-03
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
Comment
Question by:Isaac
  • 3
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 41896887
Try changing line 35 from:
this.collListItem = oList.getItems(camlQuery);

Open in new window

to
collListItem = oList.getItems(camlQuery);

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 41897334
I still get the same error.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41897338
There must be some type of conflict b/c when I comment out line 8, my code works.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41898682
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
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41898785
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now