使用Query Options分頁在Lists.GetListItems方法的介紹已經包含了,不足的是沒有給一個樣本,所以我這裡給一個使用樣本來說明。
Paging |
A string that contains data needed to support paging, including a ListItemCollectionPositionNext attribute. If left empty, the list items returned start from the beginning of the list. Client applications should supply the value of a server-supplied bookmark. The server returns the next page of data based on the bookmark supplied. This element is optional, and its default value is an empty string. |
接著接著上一篇:Query Options的一些用法(2): 包含附件路徑的樣本,增加一些測試資料。
注意分頁是需要和RowLimit來配合使用的,首先先設定下RowLimit。
static
void Main(string[] args)
{
Lists client = new
Lists();
client.Url = webUrl + "/_vti_bin/lists.asmx";
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode resultNode = client.GetListItems(listName, String.Empty, QueryAll, ViewFields, "5", QueryOptions, String.Empty);
Console.ReadLine();
}
運行得到如下結果:
<rs:data ItemCount="5" ListItemCollectionPositionNext="Paged=TRUE&p_ID=5" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="2" ows_FSObjType="1;#0" ows_Created="2012-11-30 01:37:17" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:43:10" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test02" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="2;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_UniqueId="2;#{DCDE833B-E69A-4C63-B9FE-0AE5938B3CDE}" ows_owshiddenversion="1" ows_FSObjType="2;#0" ows_Created="2012-11-30 02:48:12" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 02:48:12" ows_FileRef="2;#teams/smetechcom/Lists/QueryOptionsDemo/2_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test03" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="3;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="3" ows_UniqueId="3;#{74218240-4455-4FCA-AE83-5B966DF6F484}" ows_owshiddenversion="2" ows_FSObjType="3;#0" ows_Created="2012-11-30 03:04:00" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-11-30 03:04:45" ows_FileRef="3;#teams/smetechcom/Lists/QueryOptionsDemo/3_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test04" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="4;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="4" ows_UniqueId="4;#{2526983D-0763-43A9-8E09-5C769A4FA974}" ows_owshiddenversion="1" ows_FSObjType="4;#0" ows_Created="2012-12-03 00:46:04" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-03 00:46:04" ows_FileRef="4;#teams/smetechcom/Lists/QueryOptionsDemo/4_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test05" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="5;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="5" ows_UniqueId="5;#{02E33DB1-8EFD-4387-81E3-4476AC177E46}" ows_owshiddenversion="1" ows_FSObjType="5;#0" ows_Created="2012-12-03 00:46:05" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-03 00:46:05" ows_FileRef="5;#teams/smetechcom/Lists/QueryOptionsDemo/5_.000" xmlns:z="#RowsetSchema" />
</rs:data>
返回的Xml中只有5條資料,上面RowLimit設定相同,與前面的一些樣本不同的是,這裡多了一個ListItemCollectionPositionNext="Paged=TRUE&p_ID=5"的屬性(最後的ID=5是表示當前最後一條記錄是的ID是5)
這裡多的屬性就是需要在取下一頁資料需要傳入的參數。如下所示:
static
XmlNode QueryOptions
{
get
{
XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "<Paging ListItemCollectionPositionNext=\"Paged=TRUE&p_ID=5\"/>";
return queryOptions;
}
}
再次運行樣本程式,得到的結果就是第二頁的資料了。
<rs:data ItemCount="2" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test06" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="6;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="6" ows_UniqueId="6;#{FB18379D-2F14-4D91-B10B-F5D6F213615C}" ows_owshiddenversion="1" ows_FSObjType="6;#0" ows_Created="2012-12-03 00:46:06" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-03 00:46:06" ows_FileRef="6;#teams/smetechcom/Lists/QueryOptionsDemo/6_.000" xmlns:z="#RowsetSchema" />
<z:row ows_Title="Test07" ows_Attachments="0" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="7;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="7" ows_UniqueId="7;#{8DE5D6C5-5F6E-49A4-9A38-49537A1DD4DB}" ows_owshiddenversion="1" ows_FSObjType="7;#0" ows_Created="2012-12-03 00:46:06" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-03 00:46:06" ows_FileRef="7;#teams/smetechcom/Lists/QueryOptionsDemo/7_.000" xmlns:z="#RowsetSchema" />
</rs:data>
由於這裡資料一共不足2頁,所以第二頁就是最後一頁,也就不會看到ListItemCollectionPositionNext了。利用這樣一個特性,也就可以判斷當前頁是不是最後一頁。
使用分頁需要注意的一個問題:SharePoint的分頁由於傳入了ID,大多數情況下,ID都是不連續的(因為會存在刪除掉某些記錄的情況),會導致沒有辦法精確的跳轉到某一頁。
什麼時候會需要用到分頁呢?
我遇到過2個情境:
- 通過Web Service緩衝,處理大量資料的時候,一旦網路不穩定,會有逾時異常,這樣,需要使用分頁,每次返回一部分,最後拼成一個完整的資料。
- 大量資料的定義是需要測試來決定的。內網環境,我的經驗資料,使用者資訊表超過3W條。超過150+列的List,超過8000條資料。
- 分頁的大小也是要平衡的。要注意,每一個分頁資料都是可能擷取失敗的,過多的分頁會增大這個幾率。需要根據自己的情況確定這個分頁大小。
- 你更加關心的是效率,可以用一些非同步技術來填充資料。例如現在很流行的微博顯示風格,使用者不需要跳轉到某一頁。