集合尚未初始化 - Sharepoint Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23934781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:50:33  来源:igfitidea点击:

The collection has not been initialized - Sharepoint Javascript

javascriptsharepointsharepoint-2013

提问by Ectropy

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);

它发生在var listEnumerator = lists.getEnumerator();我看来,在我尝试将列表加载到客户端对象时出现问题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 errorwhich 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.

我看过这个问题sharepoint javascript collection not initialized error这似乎与我的非常相似,但是我在实施那里提供的解决方案时遇到了麻烦,这让我认为我的错误可能有不同的原因。

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); 

采纳答案by Ectropy

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.)

我没有检查列表是否已经存在,而是尝试创建一个列表,如果列表已经存在,则 Query 无法创建列表。(这很好,因为如果列表已经存在,我不想覆盖它。)

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());
}