该集合尚未初始化 - Sharepoint Javascript

时间:2014-05-29 13:40:27

标签: javascript sharepoint sharepoint-2013

我在尝试获取列表集合的枚举器时收到以下错误:“未捕获错误:集合尚未初始化。尚未请求或请求尚未执行。可能需要明确要求。“

它出现在var listEnumerator = lists.getEnumerator();行上,在我看来,我尝试使用context.load(lists);

将列表加载到客户端对象时出现问题

这是我的代码导致问题的部分。我在错误被抛出之前标记了这个地方。

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

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

我也尝试查询抛出错误的函数内部的列表,但这似乎无法解决任何问题。

对于一些背景知识,这些函数试图从应用程序的主机站点读取所有列表,检查是否存在指定列表,如果不存在匹配列表则创建列表。如果有一种比我正在尝试的更好的方法,我也会对此持开放态度。

任何指针?


我尝试过的一些似乎不起作用的事情:

更改异步查询

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

到同步版。

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

1 个答案:

答案 0 :(得分:1)

我已经找到了另一种方法来实现我之前尝试实现的相同目标。

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

我不完全确定我在这里做了什么不良副作用,但在我的测试中它产生了所需的行为。

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