2013-09-25 43 views
0

我有一個包含n個列數的SharePoint列表。我想獲得列表中的第5行,例如使用SharePoint Web服務。我搜索了互聯網並找到了一些代碼,但很難理解查詢語法。我不能指定列表的單行嗎?任何建議?使用Sharepoint Web服務獲取一行SharePoint列表

+0

如果是sharepoint 2010,您可以使用客戶端對象模型。 –

+0

以下任何建議是否對您有所幫助? –

+0

我發現這樣的事情,它的工作原理:query.InnerXml = 「」 + 「<值類型= \」 計數器\ 「>」 +的rowNum +「 「; –

回答

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")) 
}) 
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行。

我希望這可以幫助..

相關問題