Exercise 3: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to query the maintenance tasks SharePoint list and the Windows Azure Service created in exercise 2. You will also test the application in the local Azure AppFabric simulation environment.

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.Cloud.PartsClient.sln file located at C:\%TrainingKit%\Labs\IntegratingAzureCloudServices\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.Cloud.PartsClient 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 reader 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 WCFServiceWebRole project, right click Service References and select Add Service Reference.
  2. In the Address textbox enter the URL to the lists.asmx SharePoint web service for the site where you created the Maintenance Tasks list.

    Example: https://fbawp7/_vti_bin/lists.asmx

  3. Click Go.
  4. Once the service is resolved, enter SPListsService in the Namespace textbox.
  5. Click OK.

Task 4 – Retrieving Maintenance Requests from SharePoint

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

  1. In the WP7.Cloud.PartsClient project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 8.1.2 comment to define the LoadMaintenanceTasks method:

    C#

    public void LoadMaintenanceTasks() { XElement viewFields = new XElement("ViewFields", new XElement("FieldRef", new XAttribute("Name", "Title")), new XElement("FieldRef", new XAttribute("Name", "Body")), new XElement("FieldRef", new XAttribute("Name", "Make")), new XElement("FieldRef", new XAttribute("Name", "Model"))); WP7.Cloud.PartsClient.SPListsService.ListsSoapClient lists = new WP7.Cloud.PartsClient.SPListsService.ListsSoapClient(); lists.CookieContainer = App.CookieJar ; lists.GetListItemsCompleted += new EventHandler<WP7.Cloud.PartsClient.SPListsService. GetListItemsCompletedEventArgs>(lists_GetListItemsCompleted); lists.GetListItemsAsync(Constants.SHAREPOINT_LIST_NAME, string.Empty, null, viewFields, null, null, null); }

    The above code uses the proxy class Visual Studio 2010 generated for the lists.asmx service to query the Maintenance Tasks SharePoint list.

  3. Add the following code under the //TODO: 8.1.3 comment to define the lists_GetListItemsCompleted method:

    C#

    void lists_GetListItemsCompleted(object sender, WP7.Cloud.PartsClient.SPListsService.GetListItemsCompletedEventArgs e) { IEnumerable<XElement> rows = e.Result.Descendants(XName.Get("row", "#RowsetSchema")); IEnumerable<SPMaintenanceTask> maintenanceTasks = from element in rows select new SPMaintenanceTask { Title = (string)element.Attribute("ows_Title"), Body = Utils.HtmlToText((string)element.Attribute("ows_Body")), Make = (string)element.Attribute("ows_Make"), Model = (string)element.Attribute("ows_Model") }; Deployment.Current.Dispatcher.BeginInvoke(() => { if (MaintenanceTasks == null) { MaintenanceTasks = new ObservableCollection<SPMaintenanceTask>(); } MaintenanceTasks.Clear(); maintenanceTasks.ToList().ForEach(a => MaintenanceTasks.Add(a)); }); }

    The lists_GetListItemsCompleted method fires when the call to the lists.asmx SharePoint Web service completes. The method parses the result set, creates an instance of the SPMaintenanceTask that represents each maintenance task in the SharePoint list, and adds the SPMaintenanceTask instances to the MaintenanceTasks observable collection. The MaintenanceTasks observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the maintenance requests retrieved from the SharePoint list.

  4. Save MainViewModel.cs.

Task 5 – Adding a Reference to the Windows Azure WCF Service

In this task, you will add a reference to the PartsService Windows Azure WCF Service created in Exercise 2.

  1. In the Solution Explorer, in the WP7.Cloud.PartsClient project, right click Service References and select Add Service Reference.
  2. In the Address textbox enter the URL to the local running instance of the PartsService Windows Azure Service.

    Example: https://127.0.0.1:81/PartService.svc?wsdl

  3. Click Go.
  4. Once the service is resolved, enter PartsService in the Namespace textbox.
  5. Click OK.

Task 6 – Retrieving Replacement Part Lead-Time from Azure

In this task, you will retrieve the replacement part lead-time from the PartsService Windows Azure WCF Service.

  1. In the WP7.Cloud.PartsClient project, in the ViewModels folder, open the MaintenanceTaskViewModel.cs file.
  2. Add the following code under the //TODO: 8.1.4 comment to define the RetrieveLeadTimeFromCloud method:

    C#

    public void RetrieveLeadTimeFromCloud(string make, string model) { WP7.Cloud.PartsClient.PartsService.PartServiceClient partsService = new PartsService.PartServiceClient(); partsService.GetPartLeadTimeAsync(make, model); partsService.GetPartLeadTimeCompleted += new EventHandler<PartsService.GetPartLeadTimeCompletedEventArgs> (partsService_GetPartLeadTimeCompleted); }

    The above code uses the proxy class Visual Studio 2010 generated for the PartsService Windows Azure WCF Service to return the lead-time for a replacement part based on make and model.

  3. Add the following code under the //TODO: 8.1.5 comment to define the partsService_GetPartLeadTimeCompleted method:

    C#

    private void partsService_GetPartLeadTimeCompleted(object sender, PartsService.GetPartLeadTimeCompletedEventArgs e) { LeadTime = e.Result.ToString(); }

    The partsService_GetPartLeadTimeCompleted method fires when the call to the PartsService Windows Azure WCF Service completes, and sets the LeadTime property equal to the returned value. The LeadTime property is bound to the MaintenanceTaskView user control in the Windows Phone 7 application. The MaintenanceTaskView user control displays detailed information for a maintenance request selected in the MainPage user control. The detailed information includes the metadata about a give maintenance request from the SharePoint list, as well as the lead-time from the PartsService Windows Azure WCF Service.

  4. Save MaintenanceTaskViewModel.cs.

In this task, you will modify the web.config 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.Cloud.PartsClient 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"

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

    Figure 6

    Enable cookies on the binding

  4. Save ServiceReferences.ClientConfig.

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

    Figure 7

    CookieContainer error

    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 8 – Testing the Application in the Local Azure AppFabric Simulation Environment

In this task, you will test the Windows Phone 7 application in the local Azure AppFabric simulation environment.

  1. In the WP7.Clioud.PartsClient solution, select Windows Phone 7 Emulator in the deployment location dropdown list.
  2. Press F5.
  3. The Windows Phone application starts in the emulator and displays the items from the Maintenance Requests SharePoint list.

    Figure 8

    Maintenance Request items in the application

  4. Click on one of the maintenance requests in the list to select it.
  5. The Windows Phone application displays the details for the selected maintenance request from the SharePoint list, as well as the lead-time from the PartsService Windows Azure WCF Service.

    Figure 9

    Request details