0

我有一个包含 n 列的共享点列表。我想获取列表的第 5 行,例如使用 sharepoint Web 服务。我搜索了互联网并找到了一些代码,但很难理解查询语法。我可以不指定列表的单行吗?有什么建议吗?

4

2 回答 2

1

SPServices的语法确实不容易。我创建了一个名为SharepointPlus的 JavaScript 库,它允许执行类似 SQL 的查询。

JavaScript 代码(在您加载 jQuery 和 SharepointPlus 之后)将类似于:

var row=5; // I assume that when you say "row #5", you mean the item with the ID = 5
$SP().list("Name of your list").get({fields:"NameField",where:"ID = "+row}, function(data) {
  if (data.length===0) alert("The item with ID = "+row+" doesn't exist!")
  else alert("NameField = "+data[0].getAttribute("NameField"))
})
于 2013-09-26T08:35:55.040 回答
0

使用 SPServices,您可以使用以下代码:

$(window).ready(function () {

GetItemsFromList();
});


function GetItemsFromList()
{
 $().SPServices({
 operation: "GetListItems",
async: false,
listName: "MyTestList",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Name' /></ViewFields>",
CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>5</Value></Eq></Where></Query>",
completefunc: function (xData, Status) {
 // $(xData.responseXML).SPFilterNode("z:row").each(function() {
 $(xData.responseXML).find("z\\:row,row").each(function() {
    var liHtml = "<li>" + $(this).attr("ows_Title") + "   " +  $(this).attr("ows_Name") +  "</li>";
    $("#tasksUL").append(liHtml);
    alert(liHtml);
  });
}
  });

}

在上面的代码中,我获取了 id=5 的项目,或者您可以从列表中获取所有项目并从数据表中获取第 5 行。您可以通过将上述代码中的查询更改为以下内容来获取列表中的所有项目:

    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Name' /></ViewFields>"

您还可以使用客户端对象模型。以下是使用 ECMA 脚本的代码:

ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
function retrieveListItems()
{
   var clientContext = new SP.ClientContext();
var siteColl = clientContext.get_site();
    var oList = siteColl.get_rootWeb().get_lists().getByTitle('MyTestList');  

    var camlQuery = new SP.CamlQuery();  
   camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>5</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title'/></ViewFields></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded(sender, args) 
{
var listItemEnumerator = collListItem.getEnumerator();
var currentitemtitle = "";
while (listItemEnumerator.moveNext()) 
{
    var oListItem = listItemEnumerator.get_current();
   currentitemtitle = currentitemtitle + oListItem.get_item('Title') ;


} 


}
function onQueryFailed(sender, args) 
{
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

在上面的代码中,我从列表中获取 id 为 5 的项目。您还可以通过更改上面给出的查询来获取所有项目,然后通过遍历行来获取第 5 行。

我希望这有帮助..

于 2013-09-27T04:56:05.203 回答