Query Options的一些用法(3): 分页

使用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&amp;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&amp;p_ID=5"的属性(最后的ID=5是表示当前最后一条记录是的ID是5)

这里多的属性就是需要在取下一页数据需要传入的参数。如下所示:

static XmlNode QueryOptions

{

get

{

XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");

queryOptions.InnerXml = "<Paging ListItemCollectionPositionNext=\"Paged=TRUE&amp;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条数据。
    • 分页的大小也是要平衡的。要注意,每一个分页数据都是可能获取失败的,过多的分页会增大这个几率。需要根据自己的情况确定这个分页大小。
  • 你更加关心的是效率,可以用一些异步技术来填充数据。例如现在很流行的微博显示风格,用户不需要跳转到某一页。

 

posted @ 2012-12-03 15:27  Lambert Qin  Views(2649)  Comments(2Edit  收藏  举报