【发布时间】:2014-05-29 13:40:27
【问题描述】:
尝试获取列表集合的枚举器时出现以下错误:“未捕获的错误:集合尚未初始化。尚未请求或尚未执行请求。可能需要被明确要求。”
它发生在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);
【问题讨论】:
-
你有一个错字。您的上下文变量的名称为“currentcontext”,但是当您调用加载方法时,您使用“上下文”变量。尝试将其更改为“currentcontext”变量。
-
谢谢,但这并不能解决我的问题。这里没有看到,但我在代码的最开始创建了一个名为 context 的上下文变量——当前上下文行是多余的/不需要的,应该删除。 (我现在将编辑问题以将其删除。)
-
你的
context变量来自哪里? -
var currentContext = SP.ClientContext.get_current();在程序的最开始。
标签: javascript sharepoint sharepoint-2013