【问题标题】:Why am I getting, "Uncaught TypeError: getEnumerator is not a function"?为什么我会收到“未捕获的 TypeError:getEnumerator 不是函数”?
【发布时间】:2016-01-10 12:50:57
【问题描述】:

在我的 Sharepoint 2010 Web 部件中,我有这个 Javascript:

function getListItemID(username, payeename, oList) {
    var arrayListEnum = oList.getEnumerator();

...由此调用:

function upsertPostTravelListItemTravelerInfo1() {
var clientContext = SP.ClientContext.get_current();
var oList =   
  clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');

this.website = clientContext.get_web();
currentUser = website.get_currentUser();

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

var travelersEmail = $('traveleremail').val();

/* If this is an update, the call to getListItemID() will return a val; otherwise (an insert), get from newly instantiated ListItem.  */
listId = getListItemID(currentUser, travelersEmail, oList);

我从here 获得了此代码的基础。

但是得到了上面列出的错误(“Uncaught TypeError: oList.getEnumerator is not a function”);

一个回答说我需要添加这个:

<script type="text/javascript" src="/_layouts/15/sp.js" ></script>

...我将其从“15”更改为“14”,因为这是我们正在使用的文件夹/版本。

这不仅不起作用,而且无法识别。然后我找到了一条线索here,即添加这个:

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js"); });

...但是在已经显示的错误之前只有一个错误,即“Uncaught ReferenceError: CustomAction is not defined

那么,什么是独家新闻? getEnumerator() 或以其他方式检索我需要的 ID val 需要什么?

这是该方法的完整代码,以显示我要完成的工作以及如何完成:

function getListItemID(username, payeename, oList) {
  var arrayListEnum = oList.getEnumerator();

  while (arrayListEnum.moveNext()) {
     var listItem = arrayListEnum.get_current();

     if (listItem.get_item("ptli_formPreparedBy") === username &&
         listItem.get_item("ptli_TravelersEmail") === payeename &&
         listItem.get_item("ptli_formCompleted") == false) {
       return listItem.get_id();    
     }
   }
   return '';
}

更新

当我尝试这个时(第一行和第三行是新的):

<SharePoint:ScriptLinkID="ScriptLink1" Name="SP.js" runat="server" OnDemand="false" LoadAfterUI="true" Localizable="false"></SharePoint:ScriptLink>
<script type="text/javascript">
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

...受到猫 here 的启发,我得到了,“System.Web.HttpParseException 未被用户代码处理 Message=服务器标签格式不正确。"

就我个人而言,我认为 Sharepoint 的格式不是很好。但那是(正确的)题外话(没有双关语)。

【问题讨论】:

  • oList 返回的对象clientContext.get_web().get_lists().getByTitle() 没有getEnumerator 方法。关于调用 getByTitle() 的返回结果,SharePoint API 文档揭示了什么?
  • 我将在这里尝试更新的答案:stackoverflow.com/questions/33047426/… 它使用 getEnumerator(仍然)。我们会看看会发生什么......

标签: javascript jquery sharepoint-2010 web-parts


【解决方案1】:

问题 1:您在列表而不是列表项集合上调用 getEnumerator

getEnumerator()can only be called on a list item collection(不在 List 对象上),并且仅在通过运行 clientContext.executeQueryAsync() 填充了项目之后

问题2:需要调用executeQueryAsync来填充列表项集合

使用 SharePoint JavaScript 客户端对象模型时,您的代码需要分成两部分:第一部分指定您想要获取的内容,并涉及将查询和命令加载到 SPClientContext 对象中;第二部分允许您将查询结果操作到 SharePoint,并作为查询执行的异步回调运行。

  1. 创建上下文,指定要访问的列表等。
  2. 运行clientContext.executeQueryAsync()(其中clientContextSP.ClientContext对象),并传入委托函数以在成功或失败时运行
  3. 在“onSuccess”委托函数中,您可以使用在步骤 1 中加载的命令的结果

问题 3:您将无法直接从异步执行的函数中返回值

由于上述第 3 步是异步运行的,因此您无法从中获取返回值。任何依赖于您在第 3 步中获得的结果的逻辑都需要在执行链中向前推进,使用函数委托和回调。

问题4:列表项过滤效率低

这实际上更像是一个设计缺陷而不是一个显示停止问题,但不是让您的代码返回列表中的 每个 项,然后使用 JavaScript 枚举结果以查看是否你想要的项目在那里,你应该在 SharePoint 执行查询之前告诉它你想要什么过滤器选项。然后它只会为您提供与您的查询匹配的项目。

为此使用 CAML 查询; CAML(协作应用程序标记语言)是 SharePoint 广泛使用的一种基于 XML 的查询语言。有大量资源和工具可用于编写 CAML 查询,如果您已经创建了与查询匹配的视图,您甚至可以从 SharePoint 列表视图 Web 部件中窃取 CAML 查询。

如何使用 JavaScript CSOM 查询 SharePoint 列表的示例

这是一个使用部分代码的示例:

/* 
   ExecuteOrDelayUntilScriptLoaded(yourcode,"sp.js") makes sure 
   your code doesn't run until SP.js (the SharePoint JavaScript CSOM) 
   has been loaded
*/
ExecuteOrDelayUntilScriptLoaded(function(){
    var payeename = $('traveleremail').val();
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');

    /* Use a CAML query to filter your results */
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>'+payeename+'</Value></Eq></Where></Query></View>');

    /* get the list item collection from the list */
    var oListItems = oList.getItems(camlQuery);

    /* tell SharePoint to load the list items */
    clientContext.load(oListItems);

    /* execute the query to get the loaded items */
    clientContext.executeQueryAsync(
        /* onSuccess Function */ 
        Function.createDelegate(this,function(){
            /* 
               now that the query has run, you can get an enumerator 
               from your list item collection 
            */
            var arrayListEnum = oListItems.getEnumerator();
            var ids = [];
            while(arrayListEnum.moveNext()){
                var listItem = arrayListItem.get_current();
                ids.push(listItem.get_id());
            }
            alert(ids.length > 0 ? "IDs of matching items: " + ids : "No matching items found!");
        }),
        /*onFailure Function*/ 
        Function.createDelegate(this,function(sender,args){
            alert("Whoops: " + args.get_message() + " " + args.get_stackTrace());
        })
    );
},"sp.js");

示例代码中的 CAML 查询仅过滤 ptli_TravelersEmail 列;您需要添加一些 &lt;And&gt; 元素来捕获您想要的其他两个过滤条件。

【讨论】:

  • 我得到,“未捕获的 TypeError:camlQuery.set_viewXML 不是函数”...
  • 它现在几乎可以工作了;我得到,“未捕获的类型错误:无法读取未定义的属性'长度'。”错误在这一行: alert(listId.length > 0 ? "匹配项的 ID:" + id : "未找到匹配项!"); listId 是我在上面声明的一个全局变量(在这个函数之外)。
  • 当你声明 listId 时,尝试立即将其设置为一个空数组,如下所示:this.listId = []; 这样它就会有一个长度属性。然后,在while(arrayListEnum.moveNext()) 循环内,确保您调用的是this.listIds.push() 而不是ids.push()。另外,不要忘记在警报文本中将“匹配项的 ID:”之后的 id 更改为 this.listIds
【解决方案2】:

感谢 Thriggle,这终于对我有用了:

function setListItemID(username, payeename) {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');

    /* Use a CAML query to filter your results */
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ptli_TravelersEmail\' /><Value Type=\'Text\'>' + payeename + '</Value></Eq></Where></Query></View>');

    /* get the list item collection from the list */
    var oListItems = oList.getItems(camlQuery);

    /* tell SharePoint to load the list items */
    clientContext.load(oListItems);

    /* execute the query to get the loaded items */
    clientContext.executeQueryAsync(
        /* onSuccess Function */
        Function.createDelegate(this, function () {
            /* 
            now that the query has run, you can get an enumerator 
            from your list item collection 
            */
            var arrayListEnum = oListItems.getEnumerator();
            var ids = [];
            while (arrayListEnum.moveNext()) {
                var listItem = arrayListItem.get_current();
                ids.push(listItem.get_id());
            }
            if (ids.length > 0) {
                listId = ids[0];
            }
            else {
                listId = '';
            }
        }),
        /*onFailure Function*/
        Function.createDelegate(this, function (sender, args) {
            alert("Whoops: " + args.get_message() + " " + args.get_stackTrace());
        })
    );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多