Question

The problem is that I can't get my resources out of the ProjectContext. I can get the project names but then something goes wrong and i get this warning:

Error: 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.

My code is:

function getProjects() {

    projects = projContext.get_projects();

    projContext.load(projects);

    projContext.executeQueryAsync(projectSuccess, projectFail);


}

function projectFail(sender, args) {

    alert("Project Fail");
}

function projectSuccess() {


        var projEnum = projects.getEnumerator();


        while (projEnum.moveNext()) {

            var currProj = projEnum.get_current();
            var proj = currProj.get_id();
            console.log(proj);

            resources = currProj.get_projectResources();

            projContext.load(resources);


            projContext.executeQueryAsync((function () {resourceSucces(currProj, resources);})(currProj, resources),resourceFail);

            console.log(resources);
        }

}

function resourceFail(sender, args) {

    alert("Resource Fail");
}

function resourceSucces(currProj, resources) {

    try {

        var resourceEnum = resources.getEnumerator();

        while (resourceEnum.moveNext()) {


            var currRes = resourceEnum.get_current();
            enterpriseRes = currRes.get_enterpriseResource();
            console.log("RES " + currRes.get_id());


        }
    }
    catch (error) {

        console.log(error);

    }


}
Was it helpful?

Solution

There are some minor issues with your code but the line that's killing you is

projContext.load(projects);

Changing this to the following will make it work:

projContext.load(projects, 'Include(Id, Resources)');
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top