Exercise 2: Requesting a Page of Data

Task 1 – Adding The Client OM Code

In this task, you will add more Silverlight client object model code to the starter solution. This code will allow you to page through the data in the list 10 records at a time.

  1. If the browser is still open from Exercise 1, close it and return to Visual Studio.
  2. From Solution Explorer, in the SilverlightApplication1 project, double-click the file MainPage.xaml.cs to open it and display the code behind file for our Silverlight application.
  3. Position your cursor inside the buttonNext_Click method included in the starter solution and add the following code:

    C#

    if (null != contactItems.ListItemCollectionPosition) { qry.ListItemCollectionPosition = SetPosition(1); currentPageNumber++; LoadList(); } else { Dispatcher.BeginInvoke(() => { MessageBox.Show("End of Dataset"); }); }

  4. This code sets the current position within the resultset via the ListItemCollectionPosition property of the CamlQuery object and updates the result set by retrieving a new set of data. If the current page of data is the last page available, the method shows a message box telling the user that the end of dataset has been reached.
  5. Position your cursor inside the buttonPrevious_Click method included in the starter solution and add the following code:

    C#

    if (0 != currentPageNumber) { qry.ListItemCollectionPosition = SetPosition(-1); --currentPageNumber; LoadList(); } else { Dispatcher.BeginInvoke(() => { MessageBox.Show("Beginning of Dataset"); }); }

  6. This code is similar to the previous code except that it sets the current page back one so that when the query is run, the previous page of data is retrieved
  7. This completes the coding, so hit F5 to test out the solution. After Visual Studio launches the browser, navigate to https://intranet.contoso.com/sites/LargeData/SitePages/SilverlightApplication1WebPartPage.aspx. The page should initially look like the previous screenshot, but click on the Previous and Next buttons to test out moving forward and backward through the data, one page at a time.

Exercise 2 verification

In order to verify that you have correctly performed all steps of exercise 2, proceed as follows:

Verification 1

In this verification, you can try moving backwards and forwards through the dataset. If you are able to do this, you have completed the exercise successfully.