Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to read the Maintenance Announcement - Paging SharePoint list created in exercise 1. This application will use server-side paging to display the maintenance announcements in batches.

Task 1 – Beginning the Exercise

In this task, you will open the lab solution in Visual Studio 2010.

  1. Make sure that you have downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
  2. Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
    1. Browse to the WP7.AccAdv.Paging.sln file located at %TrainingKitPath%\Labs\PagingSharePointListDataUsingWebServices\Source\Before and select it.
    2. Click Open to open the solution.

Task 2 – Configuring Constants in the Windows Phone 7 Application

In this task, you will configure the constants used in the Windows Phone 7 application to work with your development environment.

  1. In the WP7.AccAdv.Paging project, in the Utilities folder, open the Constants.cs file.
  2. Change the value for the USER_NAME and USER_PASSWORD constants to represent a Forms Based Authentication user specific to your development environment. For this lab, the user requires read and write permissions.
  3. Change the value for the AUTHENTICATION_SERVICE_URL constant to the URL specific to your development environment.
  4. The following code example demonstrates the value for a SharePoint server named fbawp7.

    C#

    public const string AUTHENTICATION_SERVICE_URL = "https://fbawp7/_vti_bin/authentication.asmx";

Task 3 – Adding a Reference to the SharePoint Lists.asmx Web Service

In this task, you will add a reference to the SharePoint lists.asmx Web service.

  1. In the Solution Explorer, in the WP7.AccAdv.Paging project, right click Service References and select Add Service Reference.
  2. In the Addresstextbox enter the URL to the lists.asmx SharePoint Web service for the site where you created the Maintenance Announcements – Paging list.
  3. Example: https://fbawp7/_vti_bin/lists.asmx
  4. Click Go.
  5. Once the service is resolved, enter SPListsService in the Namespace textbox.
  6. Click OK.

In this task, you will modify the ServiceReferences.ClientConfig file to support the CookieContainer used with Forms BasedAuthentication. The code used to authenticate to the SharePoint server in this lab uses Forms Based Authentication. Forms Based Authentication requires the use of a CookieContainer. Please see the Security With SharePoint And Windows Phone 7 Applications Module for more information about Forms Based Authentication.

  1. In the WP7.AccAdv.Paging project, open the ServiceReferences.ClientConfig file.
  2. Locate the ListsSoap binding element.
  3. Add the following attribute to the ListsSoap binding element.

    XML

    enableHttpCookieContainer="true"

  4. The following screenshot shows what the ListSoap binding element looks like after the above code is added.

    Figure 4

    Adding the enableHttpCookieContainer attribute

    Note:
    The following exception will occur if you do not make this change to the ServiceReferences.ClientConfig file.

    Figure 5

    Expected exception when modifying a service with the enableHttpCookieContainer defined

    Note:
    If you change the interface to one or both of the services the application calls and need to update the service reference you will need to remove the XML code above from the ServiceReferences.ClientConfig file then update the service reference. After the service reference update is complete, add the XML code back to the ServiceReferences.ClientConfig file.

Task 5 – Retrieving Maintenance Announcements from SharePoint

In this task, you will use the SharePoint lists.asmx Web service to return maintenance announcements from the SharePoint list.

  1. In the WP7.AccAdv.Paging project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 5.3.1 comment to define the variables used to manage paging:

    C#

    private Stack<string> bookmarkHistory; private string nextBookMark = string.Empty; private string currentBookMark = string.Empty; private string previousBookMark = string.Empty;

    The above variables track the state of paging, allowing the application to page forward or backwards.

  3. Add the following code under the //TODO: 5.3.2 comment to define the LoadAnnouncements method:

    C# public void LoadAnnouncements(PagingDirection direction) { XElement viewFields = new XElement("ViewFields", new XElement("FieldRef", new XAttribute("Name", "Title")), new XElement("FieldRef", new XAttribute("Name", "Body"))); XElement queryOptions = new XElement("QueryOptions"); if (direction == PagingDirection.Next) { if (!string.IsNullOrEmpty(nextBookMark)) { bookmarkHistory.Push(currentBookMark); currentBookMark = nextBookMark; } } else { if (bookmarkHistory.Count > 0) { currentBookMark = bookmarkHistory.Pop(); } } if (!string.IsNullOrEmpty(currentBookMark)) { XElement rs = new XElement("Paging", new XAttribute("ListItemCollectionPositionNext", currentBookMark)); queryOptions.AddFirst(rs); } WP7.AccAdv.Paging.SPListsService.ListsSoapClient lists = new WP7.AccAdv.Paging.SPListsService.ListsSoapClient(); lists.CookieContainer = App.CookieJar; lists.GetListItemsCompleted += new EventHandler<WP7.AccAdv.Paging. SPListsService.GetListItemsCompletedEventArgs> (lists_GetListItemsCompleted); lists.GetListItemsAsync(Constants.LIST_TITLE, string.Empty, null, viewFields, Constants.PAGE_SIZE.ToString(), queryOptions, null); }

    The above code uses the proxy class Visual Studio 2010 generated for the lists.asmx Web service to retrieve a paged set of list items from the Maintenance Announcements 2 SharePoint list. The LoadAnnouncements method manages the class variables that track the next, current and history bookmarks. A bookmark is a string of data created by the lists Web service when the data sent back is truncated. The list data may be truncated when the row limit parameter is set in the GetListItemsAsync method call. If the row limit parameter is not set in the GetListItemsAsync method call the data may be truncated if the number of rows returned is limited by the view. If the view is not defined in the GetListItemsAsync method call, the default view is used.

  4. Add the following code under the //TODO: 5.3.3 comment to define the lists_GetListItemsCompleted method:

    C#

    void lists_GetListItemsCompleted(object sender, WP7.AccAdv.Paging.SPListsService.GetListItemsCompletedEventArgs e) { //Get the Paging attribute var mark = from dataRow in e.Result.Descendants(XName.Get("data", "urn:schemas-microsoft-com:rowset")) select ((string)dataRow.Attribute("ListItemCollectionPositionNext")); string bookMark = mark.FirstOrDefault(); if (string.IsNullOrEmpty(bookMark)) { nextBookMark = null; } else { nextBookMark = bookMark; } //Get the row data IEnumerable<XElement> rows = e.Result.Descendants (XName.Get("row", "#RowsetSchema")); IEnumerable<SPAnnouncement> announcements = from element in rows select new SPAnnouncement { Title = (string)element.Attribute("ows_Title"), Body = Utils.HtmlToText( (string)element.Attribute("ows_Body")) }; Deployment.Current.Dispatcher.BeginInvoke(() => { if (Announcements == null) { Announcements = new ObservableCollection<SPAnnouncement>(); } Announcements.Clear(); announcements.ToList().ForEach(a => Announcements.Add(a)); }); }

    The lists_GetListItemsCompleted method fires when the call to the GetListItemsAsync method completes. The method parses the result set to retrieve the ListItemCollectionPositionNext attribute value if it exists. The ListItemCollectionPositionNext attribute value is the “Paging Bookmark”. This value is created server-side and is a used by the GetListItemsAsync method to locate the next page of data. If the data was not truncated on the server, the ListItemCollectionPositionNext attribute will not exist in the results. If the ListItemCollectionPositionNext attribute is included in the results, it is stored in the nextBookMark variable for use in the GetListItemsAsync method call.

    The method also parses the result set, creates an instance of the SPAnnouncement class that represents each maintenance announcement in the SharePoint list, and adds the SPAnnouncement instances to the Announcements observable collection. The Announcements observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the maintenance announcements retrieved from the SharePoint list.

  5. Save MainViewModel.cs.