javascript - 该集合尚未初始化 - Sharepoint Javascript

标签 javascript sharepoint sharepoint-2013

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

它发生在 var listEnumerator =lists.getEnumerator(); 线上,在我看来,我尝试使用 context.load 将列表加载到客户端对象时出现问题(列表);

这是我的代码中导致问题的部分。我在抛出错误之前标记了该位置。

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

最佳答案

我已经找到了一种替代的、更短的方法来实现我之前试图实现的相同目标。

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

我不完全确定我在这里所做的事情是否会产生任何不良副作用,但在我的测试中它产生了所需的行为。

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

关于javascript - 该集合尚未初始化 - Sharepoint Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934781/

相关文章:

javascript - 如何判断我的网站是否导致浏览器崩溃和烧毁?

c# - 如何以编程方式确定 SharePoint 列表的大小

css - 如何修改标准对话框窗体的宽度 Sharepoint 2010

SharePoint REST 显示当前用户个人资料图片

javascript - 动态更改 SharePoint 2013 搜索代码段中的内容

javascript - 如何使用外部 AS 文件将 AS3 转换为 HTML5

JavaScript 日期到 MySQL 日期时间转换

javascript - 不允许使用负值

c# - 使用 WCF 设置 NTLM 身份验证到 Sharepoint Web 服务

javascript - 错误 : [$compile:tpload] Failed to load template: uib/template/pagination/pagination. html(HTTP 状态:404 未找到)