Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to display the contacts from the maintenance contacts SharePoint list created in exercise 1. This example uses FBA. The standard method of creating a service proxy does not work with the FBA methods used in this example. In this example, we will create the http request manually, set the FBA cookie, and then manage the processing of the REST urls. The Security With SharePoint And Windows Phone 7 Applications module discusses when a service proxy can be used with SharePoint and Windows Phone 7

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.AccBas.Rest.sln file located at %TrainingKit%\Labs\IntegratingDataUsingREST\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.AccBas.Rest, 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 – Retrieving Maintenance Contacts from SharePoint

In this task, you will use the SharePoint listsdata.svc endpoint to return maintenance contacts from the SharePoint list.

  1. In the WP7.AccBas.Rest project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 4.2.1 comment to define the LoadContacts method:

    C#

    public void LoadContacts() { string url = Constants.LIST_WEB_URL.TrimEnd('/') + "/_vti_bin/listdata.svc/MaintenanceContacts"; System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.CookieContainer = App.CookieJar; request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(GetContactsCallBack), request); }

    The above code creates a HTTPWebRequest object, sets the CookieContainer used by FBA and then makes a call to the listdata.svc page requesting the MaintainceContacts list data. Because we are using FBA in this lab we cannot use the Visual Studio DataSvcUtil.exe to create a service proxy for the Windows Phone 7 application. The current version of the phone’s DataSvcUtil.exe does not expose the request and therefore the CookieContainer cannot be attached for authentication.

  3. Add the following code under the //TODO: 4.1.2 comment to define the GetContactsCallBack method:

    C#

    private void GetContactsCallBack(IAsyncResult asyncResult) { HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); Stream content = response.GetResponseStream(); if (request != null && response != null) { if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(content)) { string responseString = reader.ReadToEnd(); ProcessDataFeed(responseString); reader.Close(); } } } }

    The GetContactsCallBack method fires when the call to the listdata.svc service completes. The result is serialized XML which is captured in the responseString variable. The string result is passed to the ProcessDataFeed method for processing.

  4. Add the following code under the //TODO: 4.1.3 comment to define the ProcessDataFeed method:

    C#

    private void ProcessDataFeed(string dataFeed) { XElement contacts = XElement.Load(new StringReader(dataFeed)); XNamespace baseNS = "https://www.w3.org/2005/Atom"; XNamespace dsNS = "https://schemas.microsoft.com/ado/2007/08/dataservices"; IEnumerable<SPContact> entries = from root in contacts.Descendants(baseNS + "entry") select new SPContact() { LastName = root.Descendants(dsNS + "LastName").FirstOrDefault().Value, FirstName = root.Descendants(dsNS + "FirstName").FirstOrDefault().Value, Company = root.Descendants(dsNS + "Company").FirstOrDefault().Value, Email = root.Descendants(dsNS + "EMailAddress").FirstOrDefault().Value, Address = root.Descendants(dsNS + "Address").FirstOrDefault().Value, City = root.Descendants(dsNS + "City").FirstOrDefault().Value, State = root.Descendants(dsNS + "StateProvince").FirstOrDefault().Value, Zip = root.Descendants(dsNS + "ZIPPostalCode").FirstOrDefault().Value, Phone = root.Descendants(dsNS + "BusinessPhone").FirstOrDefault().Value, WebSite = root.Descendants(dsNS + "WebPage").FirstOrDefault().Value, Notes = Utils.HtmlToText( root.Descendants(dsNS + "Notes").FirstOrDefault().Value), ListID = Int32.Parse(root.Descendants(dsNS + "Id").FirstOrDefault().Value) }; Deployment.Current.Dispatcher.BeginInvoke(() => { if (Contacts == null) Contacts = new ObservableCollection<SPContact>(); Contacts.Clear(); entries.ToList().ForEach(c => Contacts.Add(c)); }); }

    The ProcessDataFeed method parses the result set using Linq and creates an instance of the SPContact class that represents each maintenance contact in the SharePoint list. Each SPContact object is added to the observable collection. The Contacts observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the maintenance contacts retrieved from the SharePoint list.

  5. Save MainViewModel.cs.