使用JavaScript在SharePoint Online(2013)中检索列表项时出错

时间:2019-03-15 18:09:00

标签: sharepoint

<script type="text/javascript">

SP.SOD.executeOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
 var itemArray = [];
 var ids = [];
 var firstNames = [];
 var lastNames = [];
 var levels = [];

function retrieveListItems() {

    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle('jsTest');

   var camlQuery = new SP.CamlQuery();
    var caml = "<View>";
    caml += "<Query><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query>";
    caml += "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='FirstName'/><FieldRef Name='LastName' /><FieldRef Name='Level' /></ViewFields>";
    caml += "<RowLimit>4</RowLimit>";
    caml += "</View>";
    camlQuery.set_viewXml(caml);
    this.items = list.getItems(camlQuery);
    clientContext.load(items);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded(sender, args) {

    var itemEnumerator = items.getEnumerator();

    while (itemEnumerator.moveNext()) {
        var item = itemEnumerator.get_current();
          var id = item.get_item("ID");
          var title = item.get_item("Title");
          var firstName = item.get_item("FirstName");
          var lastName = item.get_item("LastName");
          var level = item.get_item("Level");
          itemArray.push(id + ", " + title + ", " + firstName + ", " + lastName + ", "+ level);
          ids.push(id);
          firstNames.push(firstName);
          lastNames.push(lastName);
          levels.push(level);


      }

         document.getElementById("test2").innerHTML = itemArray;
         document.getElementById("test3").innerHTML = ids;
         document.getElementById("test4").innerHTML = firstNames;
         document.getElementById("test5").innerHTML = lastNames;
         document.getElementById("test6").innerHTML = levels;
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<div class="test1">
  <p id="test2">Undefined</p>
</div>
<div class="test1">
  <p id="test3">Undefined</p>
</div>
<div class="test1">
  <p id="test4">Undefined</p>
</div>
<div class="test1">
  <p id="test5">Undefined</p>
</div>
<div class="test1">
  <p id="test6">Undefined</p>
</div>

大家好, 我正在尝试检索一些列表项,并在我们的SharePoint Team网站中将其显示为HTML文件。 我已经使用了下面的代码,但出现了一些错误...屏幕上没有数据显示,如在截取的屏幕上看到的那样 Html page captured

这是我用来检索:SharePoint List

上的数据的列表

以下是页面加载时chrome浏览器控制台中显示的错误:Errors from Chrome Browser Console

能否请您解决此问题?我真的不知道在代码或SharePoint网站上出了什么问题。

有什么办法可以解决这个问题?

2 个答案:

答案 0 :(得分:0)

修改后的代码如下:

<script type="text/javascript">

SP.SOD.executeOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
 var itemArray = [];
 var ids = [];
 var firstNames = [];
 var lastNames = [];
 var levels = [];

function retrieveListItems() {

    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle('jsTest');

   var camlQuery = new SP.CamlQuery();
    var caml = "<View>";
    caml += "<Query><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query>";
    caml += "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='FirstName'/><FieldRef Name='LastName' /><FieldRef Name='Level' /></ViewFields>";
    caml += "<RowLimit>4</RowLimit>";
    caml += "</View>";
    camlQuery.set_viewXml(caml);
    this.items = list.getItems(camlQuery);
    clientContext.load(items);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

function onQuerySucceeded(sender, args) {

    var itemEnumerator = items.getEnumerator();

    while (itemEnumerator.moveNext()) {
        var item = itemEnumerator.get_current();
          var id = item.get_item("ID");
          var title = item.get_item("Title");
          var firstName = item.get_item("FirstName");
          var lastName = item.get_item("LastName");
          var level = item.get_item("Level");
          itemArray.push(id + ", " + title + ", " + firstName + ", " + lastName + ", "+ level);
          ids.push(id);
          firstNames.push(firstName);
          lastNames.push(lastName);
          levels.push(level);


      }

         document.getElementById("test2").innerHTML = itemArray;
         document.getElementById("test3").innerHTML = ids;
         document.getElementById("test4").innerHTML = firstNames;
         document.getElementById("test5").innerHTML = lastNames;
         document.getElementById("test6").innerHTML = levels;
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<div class="test1">
  <p id="test2">Undefined</p>
</div>
<div class="test1">
  <p id="test3">Undefined</p>
</div>
<div class="test1">
  <p id="test4">Undefined</p>
</div>
<div class="test1">
  <p id="test5">Undefined</p>
</div>
<div class="test1">
  <p id="test6">Undefined</p>
</div>

enter image description here

enter image description here

我已经回答了您在Technet中发布的相同问题,您也可以在此处进行检查,如果有帮助,请记住也要在Technet中标记:

Not Able to retrieve more than two column (ID an Title) from SharePoint list using JavaScript

答案 1 :(得分:0)

我相信一个或多个字段的内部名称与您在代码/ CAML查询中编写的名称不同。

请仔细检查您在CAML查询中使用的所有字段的内部名称以及代码。

这应该可以解决错误。