Javascript 如何使用 Jquery 检索共享点列表数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14909070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:17:45  来源:igfitidea点击:

How Retrieve Sharepoint List data using Jquery

javascriptjquerysharepointsharepoint-2013

提问by Rahul Gokani

I am trying to retrieve list data using Jquery. But I am getting the whole list Html page not the values from the List. Jquery Code is as follows

我正在尝试使用 Jquery 检索列表数据。但我得到的是整个列表 Html 页面,而不是列表中的值。jquery代码如下

<script src="../_layouts/15/SharePointProject1/Scripts/jquery-1.3.2.js"></script>
<script language = "javascript" type="text/javascript">
    function GetAnnouncementData() {
        var soapPacket = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
   <soapenv:Body> \
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
     <listName>Temp</listName> \
     <viewFields> \
      <ViewFields> \
        <FieldRef Name='Title' /> \
      </ViewFields> \
     </viewFields> \
    </GetListItems> \
   </soapenv:Body> \
  </soapenv:Envelope>";
        jQuery.ajax({
            url: "http://serverName/Lists/Temp/listsView.aspx",
            type: "POST",
            dataType: "xml",
            data: soapPacket,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
    }

    function processResult(xData, status) {
        //jQuery(xData.responseXML).find("z\:row").each(function () {
        //    $("<li>" + $(this).attr("ows_Title") + "</li>").appendTo("#AnnouncementData");
        //});
        //alert($(this).attr("ows_Title"));
        alert(xData.responseText);
        //alert(status.toString());
        document.getElementById("tarea").value = xData.responseText;
        document.getElementById("div1").innerHTML = xData.responseText;
    }
    $(document).ready(function () {
        GetAnnouncementData();
    });
</script>

This code is giving me The list page not list data. I tried to get xml file in div tag so that I can view what it is returning me. It returns me the HTML page of that list.
Please Help.

这段代码给了我列表页面而不是列表数据。我试图在 div 标签中获取 xml 文件,以便我可以查看它返回给我的内容。它返回该列表的 HTML 页面。
请帮忙。

采纳答案by Mital

Did you check your URL that you have given to access the list.

您是否检查了您提供的用于访问列表的 URL。

url: "http://serverName/Lists/Temp/listsView.aspx",

In this URL if you are accessing the local machine then you have to give localhost in server name.

在此 URL 中,如果您正在访问本地计算机,则必须在服务器名称中提供 localhost。

url: "http://localhost:80/Lists/Temp/listsView.aspx",

Try using this and let me know if it works or not.

尝试使用它并让我知道它是否有效。

回答by jfplataroti

For me the best option is using REST calls receiving the data as json, is much more cleaner and easier to use.

对我来说,最好的选择是使用 REST 调用以 json 格式接收数据,这样更简洁、更易于使用。

Also, the good thing is that REST calls on SharePoint are made using Linq, so in this way is more flexible when you create the query, like for example you can do a better pagination, instead of using the crappy paginations of sharepoint that only allow you to do a next items.

此外,好消息是 SharePoint 上的 REST 调用是使用 Linq 进行的,因此在创建查询时以这种方式更加灵活,例如,您可以进行更好的分页,而不是使用仅允许的糟糕的 sharepoint 分页你做下一个项目。

var ajaxCall = $.getJSON("http://SharePointSiteUrl/_vti_bin/listdata.svc/ListName");

ajaxCall.success(function (jsonData) { callbackSuccessFunction(jsonData.d.results); });
ajaxCall.error(function () { callbackErrorFunction({ status: 'error', data: 'error message.' }); });

The returned data will be received as json in the following format:

返回的数据将以以下格式作为 json 接收:

{d: {results: [arrays of items]}

In case you need to know the name of the list to do the rest call, just call the url:

如果您需要知道列表的名称以进行其余调用,只需调用 url:

http://SharePointSiteUrl/_vti_bin/listdata.svc/

Here is some info: http://msdn.microsoft.com/en-us/library/ff798339.aspx

这是一些信息:http: //msdn.microsoft.com/en-us/library/ff798339.aspx

I hope it helps.

我希望它有帮助。

回答by Roberto

An old question, but to help others looking for an answer ...

一个老问题,但为了帮助其他人寻找答案......

The simplest way to "retrieve list data using jQuery" is to use the SharePoint URL protocol. It can return list data in XML and doesn't require you to build SOAP requests. All you need is the list GUID and the ows_ attribute names (use FireBug to inspect the returned XML).

“使用 jQuery 检索列表数据”的最简单方法是使用 SharePoint URL 协议。它可以以 XML 格式返回列表数据,并且不需要您构建 SOAP 请求。您只需要列表 GUID 和 ows_ 属性名称(使用 FireBug 检查返回的 XML)。

The simple example below will display announcement data. Paste the code into an empty HTML page on your SharePoint site and replace the siteUrland listIdvalues with your own.

下面的简单示例将显示公告数据。在您的SharePoint网站的代码到一个空的HTML页面粘贴和更换SITEURLlistId用自己的价值观。

Microsoft Reference: URL Protocol

Microsoft 参考:URL 协议

If you need to do more than just read list data then have a look at jQuery.SPServicesby Marc D Anderson.

如果您需要做的不仅仅是读取列表数据,请查看 Marc D Anderson 的jQuery.SPServices

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">

/* Tested with WSS3,IE9,FF19 */

$(document).ready( function() {

    var siteUrl = "//london",
        listId  = "{1EC2EC89-B3DC-434D-A63E-AAEBF72E4E89}";

$.get( siteUrl + "/_vti_bin/owssvr.dll?Cmd=Display&XMLDATA=TRUE&List=" + listId, 
    function( xml ) {
        var zrow = xml.getElementsByTagName("z:row");
        for(var i=0; i<zrow.length; i++) {
            $("#table1 tbody").append( 
            "<tr><td>"
            + zrow[i].getAttribute("ows_Attachments")
            + "</td><td>"
            + zrow[i].getAttribute("ows_LinkTitle") 
            + "</td><td>" 
            + zrow[i].getAttribute("ows_Body") 
            + "</td></tr>" 
            );
        }
});
});

</script>

<table id="table1">
<caption>Announcements</caption>
<tbody></tbody>
</table>

</body>
</html>