简体   繁体   English

使用SPServices从SharePoint中读取

[英]Read from SharePoint with SPServices

I am trying to read from a SharePoint list("Winners") and display it on a page using content editor. 我正在尝试从SharePoint列表(“优胜者”)中读取内容,并使用内容编辑器将其显示在页面上。 This sounds really simple but for some reason I'm having a hard time with this. 这听起来真的很简单,但是由于某些原因,我对此很难。 My code to do this is below: 我的代码如下:

<style type="text/css">
  h4{
  color: red

}
table{
border: #ccc solid 2px;
border-radius: 3px;

}

</style>

<script src="/_layouts/1033/FH/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/_layouts/1033/FH/jquery.SPServices-0.7.1a.js">
</script>

<script type="text/javascript">
$(document).ready(function(){
    GetWinners();

}):

//getWinners Function starts here.
function GetWinners(){

    //Variables to store information.
    var method = "GetListItems";
    var list = "Winners";
    var fieldsToRead =  "<ViewFields>" +
                            "<FieldRef Name='Title' />" +
                            "<FieldRef Name='Picture' />" +
                            "<FieldRef Name='Text' />" +
                        "</ViewFields>";

    var query = "<Query>" +
                    "<Where>" +
                        "<Neq>" +
                            "<FieldRef Name='ID'/><Value Type='Number'>0</Value>" + 
                        "</Neq>" +
                    "</Where>" +
                    "<OrderBy>" + 
                        "<FieldRef Name='Title'/>" +
                    "</OrderBy>" +
                "</Query>";

    //SPServices call where we pass the above variables.
    $().SPServices({
        operation: method,
        async: false,
        listName: list,
        CAMLViewFields: fieldsToRead,
            CAMLQuery: query,
                completefunc: function (xData, Status){
                     $(xData.responseXML).SPFilterNode("z:row").each(function() { 
                        var name = ($(this).attr("ows_Title"));
                        var pictureUrl = ($(this).attr("ows_Picture"));
                        var caption = ($(this).attr("ows_Text"));

                        AddRowToTable(name, pictureUrl, caption);

                    });

                }

    });
}//End

//Function to add content on the page starts here.
function AddRowToTable(name, pictureUrl, caption){
    $("#winnersTable").append("<tr align:'middle'>" +
                                "<td><img src='"+ pictureUrl + "'/><br><h4>" + name + "</h4><br><p>" + caption + "</p></td>" +
                               "</tr>");

}

</script>

<!-- table where the winners would go -->
<div>
<table id="winnersTable"></table>
</div>

It will be a great help if someone could point out what I'm doing wrong. 如果有人可以指出我在做什么错,那将是一个很大的帮助。 I have all the internal name of fields and list correct, so that's not the issue. 我具有字段的所有内部名称,并且列表正确,所以这不是问题。 Perhaps I'm missing up when I try to reference it from content editor. 当我尝试从内容编辑器中引用它时,我可能会想念它。 I hope I explained this right, but please let me know if there is any questions. 希望我解释了这一权利,但是如果有任何疑问,请告诉我。

Regards, 问候,

There is a typo at line: 在行上有一个错字:

}):

It is supposed to be semicolon instead of colon. 应该是分号而不是冒号。

Some recommendations 一些建议

Place markup in CEWP, but not a JavaScript code 将标记放置在CEWP中,而不是JavaScript代码中

In fact it depends, but still there are some Pros here. 实际上,这取决于情况,但是这里仍然有一些优点。 First of all it concerns the maintenance and support of JavaScript code base 首先,它涉及JavaScript代码库的维护和支持。

Usage of common libraries 通用库的用法

It is recommended to reference a JavaScript libraries (like jQuery ) from a master pages rather than from web parts. 建议从主页而不是Web部件引用JavaScript库(如jQuery )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM