设为首页 收藏本站
查看: 672|回复: 0

[经验分享] SharePoint List Pagination using SPListItemCollectionPosition

[复制链接]
发表于 2015-9-24 12:16:25 | 显示全部楼层 |阅读模式
SharePoint List Pagination using SPListItemCollectionPosition
  Description
  This article will show you how to apply paging with sorting on search grid which gets data from sharepoint list by using SPQuery and SPListItemCollectionPosition.
  Requirement:-
  I have search page which is divided in 2 part; Search filter criteria and other is display result in grid. This page query to sharepoint list using object model and display result.
By thinking of performance; you will never try to get all results from sharepoint list. You have to query page by page. I mean query sharepoint list only for 10 page (page size=10) and display data.
  Here I am giving detail about how to query for 10 page and get data. Then display pagination.
  Precisely my page will do below things:-
  1. Put caml query (only for page size which is set to 10 in my example) on sharepoint list
2. Filter on folder basis
3. Apply sorting
4. Display paging on search result page.
  My hunt On Google:-
  I have search on google lots but no where I find proper solution which handle sorting, paging with filter using caml query and folder query.
But still I like msdn and below 2 blogs from where I got basic understanding
http://www.directsharepoint.com/2011/03/step-by-step-guide-to-implement-paging.html
http://blogs.msdn.com/b/colbyafrica/archive/2009/02/19/learning-sharepoint-part-vi-list-pagination.aspx
  The Code:
  Below are 2 classes SearchDocuments and SearchPaging.
SearchDocuments: This class is code behind file my user control which manage search.
SearchPaging: This class set property for search results and search paging.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244


public partial class SearchDocuments : System.Web.UI.UserControl
{
public string Next
{
get { return ViewState["Next"] == null ? string.Empty : (string)ViewState["Next"]; }
set { ViewState["Next"] = value; }
}
public string Previous
{
get { return ViewState["Previous"] == null ? string.Empty : (string)ViewState["Previous"]; }
set { ViewState["Previous"] = value; }
}
// store column name for order by query
private string DataSortExpression
{
get { return ViewState["DataSortExpression"] == null ?
Constant.SearchResultColumnDocumentName : (string)ViewState["DataSortExpression"]; }
set { ViewState["DataSortExpression"] = value; }
}
// store direction; Acending or decending for order by query
private SortDirection DataSortDirection
{
get { return ViewState["DataSortDirection"] == null ?
SortDirection.Ascending : (SortDirection)ViewState["DataSortDirection"]; }
set { ViewState["DataSortDirection"] = value; }
}
/// <summary>
/// This function get call on search button and prev or next link
/// this manage paging and grid results.
/// </summary>
private void BindListData(Dictionary<string, string> searchFilters,
string pagingInfo, int currentPageNumber)
{
try
{
ViewState["CurrentPage"] = currentPageNumber.ToString();
uint rowCount = 10; //Page Size
// below 2 string format are good if you have orderby in your caml query.
string nextPageString = "Paged=TRUE&p_FSObjType=0&p_{0}={1}&p_ID={2}";
string PreviousPageString = "Paged=TRUE&p_FSObjType=0&PagedPrev=TRUE&p_{0}={1}&p_ID={2}";
/*
below 2 string format are good if you dont have orderby in your caml query.
string nextPageString = "Paged=TRUE&p_{0}={1}&p_ID={2}";
string PreviousPageString = "Paged=TRUE&PagedPrev=TRUE&p_{0}={1}&p_ID={2}";
*/
int pageOrderNumber = ((currentPageNumber - 1) * Convert.ToInt32(rowCount)) + 1;
string orderBy = string.Empty;
// set order by query.
// By default DataSortExpression value is ID. but on click of sorting it change column value
if (DataSortDirection == SortDirection.Ascending)
orderBy = @"<OrderBy><FieldRef Name='" +
DataSortExpression + "' Ascending='True' /></OrderBy>";
else
orderBy = @"<OrderBy><FieldRef Name='" +
DataSortExpression + "' Ascending='False' /></OrderBy>";
// it generate caml query based on my search filters
// you can write you own logic to build caml query
string query = GetQuery(searchFilters);
query = string.Concat(query, orderBy); // add order by in query
// get folder name from you search filter criteria
string folderName = searchFilters.TryGetValue("FolderName", out folderName);
// pass paging info, query row limit in below function
// this function will return object of my custom class which hold datatable object and paging info
// Once current function ends; You will find detail about GetAllSearch function in this blog
SearchPaging searchData = GetAllSearch(query, pagingInfo, rowCount, pageOrderNumber, folderName);
if (searchData != null && searchData.SearchItems != null)
{
SPListItemCollectionPosition itemPosition = searchData.ItemCollPosition;
// ctlGrid is a object of my SPGridView
ctlGrid.DataSource = searchData.SearchItems;
ctlGrid.DataBind();
this.ResultPanel_Search.Visible = true;
//now we need to identify if this is a call from next or first
if (null != itemPosition)
{
nextPageString =
string.Format(nextPageString, DataSortExpression,
archData.LastItem[DataSortExpression], searchData.LastItem.ID);
}
else
{
nextPageString = string.Empty;
}
if (currentPageNumber > 1)
{
PreviousPageString =
string.Format(PreviousPageString, DataSortExpression,
searchData.FirstItem[DataSortExpression], searchData.FirstItem.ID);
}
else
{
PreviousPageString = string.Empty;
}
if (string.IsNullOrEmpty(nextPageString))
{
LinkButtonNext.Visible = false;
}
else
{
LinkButtonNext.Visible = true;
}
if (string.IsNullOrEmpty(PreviousPageString))
{
LinkButtonPrevious.Visible = false;
}
else
{
LinkButtonPrevious.Visible = true;
}
ViewState["Previous"] = PreviousPageString;
ViewState["Next"] = nextPageString;
string pageNumber = string.Empty;
if (currentPageNumber == 1)
pageNumber = Convert.ToString(((currentPageNumber - 1) * Convert.ToInt32(rowCount)) + 1);
else
pageNumber = Convert.ToString((currentPageNumber));
lblPaging.Text = "Page - " + pageNumber;
ctlContainerUpdatePanel.Update();
}
else
{
ctlContainerUpdatePanel.Update();
this.ResultPanel_Search.Visible = false;
this.ErrorMessageLabel.Text = "No records found";
}
}
catch (Exception ex)
{
// manage exception
}
}
/// <summary>
/// This function query to sharepoint list and return SearchPaging class object
/// which holde results and paging info
/// </summary>
private SearchPaging GetAllSearch(string queryXML, string pagingInfo, uint rowCount,
int pageOrderNumber, string folderName)
{
SPQuery query = new SPQuery();
query.Query = queryXML;
query.RowLimit = rowCount;
if (!string.IsNullOrEmpty(pagingInfo))
{
SPListItemCollectionPosition position = new SPListItemCollectionPosition(pagingInfo);
query.ListItemCollectionPosition = position;
}
query.ViewFields = string.Concat(
"<FieldRef Name='ID' />",
"<FieldRef Name='Title' />");
// assign folder name
if (!String.IsNullOrEmpty(folderName))
{
SPFolder searchFolder = list.RootFolder.SubFolders[folderName];
query.Folder = searchFolder;
query.ViewAttributes = "Scope="Recursive"";
}
SPListItemCollection itemColl = list.GetItems(query);
// SearchPaging is my custom class which have some get set.
// i have copied code of this class in bottom for your reference
SearchPaging searchPaging = new SearchPaging(itemColl.GetDataTable()); // set result datatable
searchPaging.ItemCollPosition = itemColl.ListItemCollectionPosition; // set position object
searchPaging.FirstItem = itemColl[0]; // set first item of page
searchPaging.LastItem = itemColl[itemColl.Count - 1]; // set last item of page
return searchPaging;
}
protected void linkBtn_Search_Click(object sender, EventArgs e)
{
try
{
ViewState["Next"] = string.Empty;
ViewState["Previous"] = string.Empty;
ViewState["DataSortExpression"] = Constant.SearchResultColumnDocumentName;
// get filters and store into Dictionary object
// i store actual internal column value as key, so that i can use in my caml query
Dictionary<string, string> searchFilters = GetSearchData();
if (searchFilters.Count > 0)
{
BindListData(searchFilters, ViewState["Next"] as string, 1);
}
}
catch(Exception ex)
{
// manage exception
}
}
protected void LinkButtonPrevious_Click(object sender, EventArgs e)
{
Dictionary<string, string> searchFilters = GetSearchData();
if (searchFilters.Count > 0)
BindListData(searchFilters, ViewState["Previous"] as string, Convert.ToInt32(ViewState["CurrentPage"]) - 1);
}
protected void LinkButtonNext_Click(object sender, EventArgs e)
{
Dictionary<string, string> searchFilters = GetSearchData();
if (searchFilters.Count > 0)
BindListData(searchFilters, ViewState["Next"] as string, Convert.ToInt32(ViewState["CurrentPage"]) + 1);
}      
}
public partial class SearchPaging
{
public SPListItemCollectionPosition ItemCollPosition { get; set; }
public SPListItem FirstItem { get; set; }
public SPListItem LastItem { get; set; }
private DataTable searchItems;
public int SearchResultCount { get; set; }
public DataTable SearchItems
{
get{return searchItems;}
}
public SearchPaging() { }
public SearchPaging(DataTable SearchItems)
{
searchItems = SearchItems;
}
}
  Summary
  In this i felt all things are simple. Only Paging info, list item position, first item and last item need to set properly.
  Hope this helps.
Thanks!
Avinash
  resource:http://sharepoint.infoyen.com/2012/03/06/sharepoint-list-pagination-using-splistitemcollectionposition/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118205-1-1.html 上篇帖子: 一步一步配置MicroSoft SharePoint 2013企业版(扫盲贴) 下篇帖子: Sharepoint 2013设置customErrors
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表