1

I'm getting the following error when attempting to get an enumerator for a collection of lists: "Uncaught 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."

It happens on the line var listEnumerator = lists.getEnumerator(); it seems to me that there is an issue in my attempt to load lists into the client object with context.load(lists);

Here's the portion of my code that's causing the problem. I've marked the place just before the error is thrown.

//____________________________Required function for accessing the host site's info.___________________________________
function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");    
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == param) {
        return singleParam[1];
}   
}
}

//____________________________Begin checking for list_________________________
function checkForList(listToFind, typeOfListToCreateIfTheListIsMissing)
{
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostcontext = new SP.AppContextSite(context, hostUrl);
    var hostweb = hostcontext.get_web();

    var lists = hostweb.get_lists();
    context.load(lists);
    context.executeQueryAsync(checkIfListExistsUsingEnumerator(listToFind, lists, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed);
}
//Failed to get lists for some reason
function onQueryFailed(sender, args) {  
    alert('We failed to retrieve lists. \n' + args.get_message() + '\n' + args.get_stackTrace());  
}

//____________________________Does list exist?____________________________
function checkIfListExistsUsingEnumerator(listToFind, lists, hostweb, typeOfList)
{   
    var listExists = false; 
    //!!!!!!!!!!!!!!! ERROR HERE !!!!!!!!!!!!!!!!       
    var listEnumerator = lists.getEnumerator();
    var title;
    while (listEnumerator.moveNext()) 
    {
        title = listEnumerator.get_current().get_title();  

        if (title == listToFind)
        {  
            listExists = true;              
        }  
    }

    if (!listExists) 
    {
        alert("It appears that a required list does not already exist. \nClick ok, and we'll automatically create one for you.");
        //Create a new list
        createList(listToFind, hostweb, typeOfList); 
    }
    else if (listExists)
    {
        //Do nothing.
    }         
}

//____________________________If it doesn't, create one on the local site____________________________
function createList(nameOfNewList, hostweb, typeOfList) {
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(nameOfNewList);

    if (typeOfList === "events")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.events);
    }
    else if (typeOfList === "contacts")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.contacts);
    }
    var lists = hostweb.get_lists();
    var newList = lists.add(listCreationInfo);
    context.load(newList);
    context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
}

function onListCreationSuccess() {
    alert('List created successfully!');
}

function onListCreationFail(sender, args) {
    alert('Failed to create the list. ' + args.get_message());
}

I've looked at this question sharepoint javascript collection not initialized error which seems to be fairly similar to mine, but I'm having trouble implementing the solution provided there, making me think my error may be have a different cause.

I've also tried querying for the lists inside of the function that is throwing the error, but that doesn't seem to solve anything.

For a little background, these functions are attempting to read all lists from the app's host site, check to see if a specified list exists, and create a list if no matching list exists. If there's a better way of doing that than what I'm attempting, I'd be open to that too.

Any pointers?


Some things I've tried that don't seem to work:

Changing the Asynchronous query

context.executeQueryAsync(checkIfListExists(listToFind, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed);

to a Synchronous one.

context.executeQuery(checkIfListExists(listToFind, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed); 
Community
  • 1
  • 1
Ectropy
  • 1,533
  • 4
  • 20
  • 37
  • You have a typo. You context variable has name "currentcontext" but when you call load method you use "context" variable. Try to change it to "currentcontext" variable. – Yevgeniy.Chernobrivets May 29 '14 at 16:12
  • Thanks, but that doesn't fix my issue. It's not seen here, but I create a context variable called context at the very beginning of the code--that currentcontext line is redundant/unneeded and should be removed. (I'll edit the question to remove it now.) – Ectropy May 29 '14 at 16:36
  • where is your `context` variable coming from? – c0deNinja May 29 '14 at 23:31
  • `var currentContext = SP.ClientContext.get_current();` at the very beginning of the program. – Ectropy May 30 '14 at 12:53

1 Answers1

1

I've figured out an alternate, and shorter way to method of achieving the same goal I was trying to achieve before.

Instead of checking to see if a list does not already exist, I just try to create a list, and the Query fails to create a list if one is already there. (That's good because I don't want to overwrite the list if it is already there.)

I'm not totally sure if there are any undesired side effects of what I'm doing here, but in my tests it produced the desired behavior.

//____________________________Required function for accessing the host site's info.___________________________________
function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");    
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == param) {
        return singleParam[1];
}   
}
}

//____________________________Create a list if one does not already exist_________________________
function createList(listToCreate, typeOfList)
{
    // Create an announcement SharePoint list with the name that the user specifies.
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);
    var hostweb = hostContext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();

    listCreationInfo.set_title(listToCreate);

    if (typeOfList === "events")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.events);
    }
    else if (typeOfList === "contacts")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.contacts);
    }
    var lists = hostweb.get_lists();
    var newList = lists.add(listCreationInfo);
    currentContext.load(newList);
    currentContext.executeQueryAsync(onListCreationSuccess, onListCreationFail);     
}

function onListCreationSuccess() {
    alert("We've created a list since one doesn't exist yet." );
}

function onListCreationFail(sender, args) {
    alert("We didn't create the list. Here's why: " + args.get_message());
}
Ectropy
  • 1,533
  • 4
  • 20
  • 37