{"id":804,"date":"2010-10-31T16:12:04","date_gmt":"2010-10-31T15:12:04","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=804"},"modified":"2015-09-08T20:34:37","modified_gmt":"2015-09-08T18:34:37","slug":"paging-with-client-om-and-caml","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/paging-with-client-om-and-caml\/","title":{"rendered":"Paging with Client OM and CAML"},"content":{"rendered":"<p>CAML can be used in the Client OM to query for example a list. When the list contains a lot of items it&#8217;s not a good idea to show all the items at once. CAML supports row limits, so a\u00a0 &lt;RowLimit&gt; element can be passed in the CAML query and show a subset of the results.<br \/>\nMaybe the user wants to browse through all the results, so all items has to be displayed on the page in some sort of form. Here paging of the results come in handy.\u00a0\u00a0<\/p>\n<p>To use paging, the ListItemCollectionPosition property of the ListItemCollection object can be used.<br \/>\nTo keep track of paging the ListItemCollectionPosition of the CAML query has to be set to the ListItemCollectionPosition of your own object. This way the position of the starting point of the query can be set before querying the list and iterate through the pages until there are no pages left anymore.\u00a0\u00a0<\/p>\n<p>For testing purposes a Windows Forms application is created, with 2 buttons and a listbox.<br \/>\nThe first button, named Start, gets the first 500 items of a list and displays the result as one item in the listbox. By pressing the second button, named Next, the next 500 results will be displayed until there are no pages left to display. When there are no pages left, the Next button is disabled. By pressing the Start button the process can be started again at the beginning.\u00a0\u00a0<\/p>\n<p>In this example a list called LargeList is used, the list contains 2500 items.<br \/>\nThe CAML query used gets the Title field of an item and set the RowLimit to 500 items. After getting the items the ListItemCollectionPosition is stored in a property called itemPosition to keep track of the position of the starting point of the next query.<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/ListItemCollectionPosition.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-805\" title=\"ListItemCollectionPosition\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/ListItemCollectionPosition.png\" alt=\"\" width=\"601\" height=\"105\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/ListItemCollectionPosition.png 751w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/ListItemCollectionPosition-300x52.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a>\u00a0\u00a0<\/p>\n<p>As soon as the itemPosition equals null, the results of the last page are gathered.\u00a0\u00a0<\/p>\n<p>The example code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public Microsoft.SharePoint.Client.ListItemCollectionPosition itemPosition { get; set; }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public ClientContext context { get; set; }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public Form1()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InitializeComponent();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void button1_Click(object sender, EventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 listBoxResults.Items.Clear();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 buttonNext.Enabled = true;\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GetPage();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void GetPage()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List largeList = context.Web.Lists.GetByTitle(&quot;LargeList&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CamlQuery query = new CamlQuery();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 query.ListItemCollectionPosition = itemPosition;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 query.ViewXml = &quot;&lt;View&gt;&lt;FieldRef Name='Title' \/&gt;&lt;RowLimit&gt;500&lt;\/RowLimit&gt;&lt;\/View&gt;&quot;;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ListItemCollection collection = largeList.GetItems(query);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context.Load(collection);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context.ExecuteQuery();\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 itemPosition = collection.ListItemCollectionPosition;\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string result = string.Empty;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (ListItem item in collection)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result += item&#x5B;&quot;Title&quot;].ToString();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (itemPosition == null)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 listBoxResults.Items.Add(result + Environment.NewLine + &quot; Position: Last page &quot; + collection.Count + &quot; items&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 buttonNext.Enabled = false;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 listBoxResults.Items.Add(result + Environment.NewLine + &quot; Position: &quot; + itemPosition.PagingInfo);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void buttonNext_Click(object sender, EventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 GetPage();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void Form1_FormClosed(object sender, FormClosedEventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context.Dispose();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void Form1_Load(object sender, EventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context = new ClientContext(&quot;&lt;your_context&gt;&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\r\n\r\n<\/pre>\n<p>\u00a0<\/p>\n<p>And the Windows Forms form:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-806\" title=\"FormListItemCollectionPosition1\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition1.png\" alt=\"\" width=\"422\" height=\"141\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition1.png 422w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition1-300x100.png 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/a>\u00a0<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-807\" title=\"FormListItemCollectionPosition2\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition2.png\" alt=\"\" width=\"505\" height=\"145\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition2.png 505w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/10\/FormListItemCollectionPosition2-300x86.png 300w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CAML can be used in the Client OM to query for example a list. When the list contains a lot of items it&#8217;s not a good idea to show all the items at once. CAML supports row limits, so a\u00a0 &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/paging-with-client-om-and-caml\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[20,42,6],"class_list":["post-804","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-c","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=804"}],"version-history":[{"count":8,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":813,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/804\/revisions\/813"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}