Share via


Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to read from the list created in Exercise 1 and store the results in isolated storage. When network connectivity is not available the application will read the announcements from isolated storage.

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.SharePoint.Offline.sln file located at %TrainingKitPath%\Labs\Tips\Offline\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.SharePoint.Offline 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.

    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.SharePoint.Offline 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 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.

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.SharePoint.Offline 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 4

    Add Cookie Support to the Binding element

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

    Figure 5

    Error if cookies are not enabled

    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 – Creating a Static Property to Store Network Connectivity Status

In this task, you will create an application-level static property to maintain the current network connectivity status.

  1. In the WP7.SharePoint.Offline project, right-click on App.Xaml and select View Code.
  2. Add the following code under the //TODO: 10.1.1 comment to define the IsNetworkAvaialble property:

    C#

    public static bool IsNetworkAvailable { get; set; }

    This static property is stores the network connectivity state for the Windows Phone 7 mobile device.

  3. Save the App.Xaml.cs.

Task 6 – Retrieving and Storing Maintenance Announcements from SharePoint

In this task, you will use the SharePoint lists.asmx Web service to return maintenance announcements from the SharePoint list. The results will be stored in isolated storage.

  1. In the WP7.SharePoint.Offline project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 10.1.2 comment to define the LoadAnnouncementsFromSource method:

    C#

    private void LoadAnnouncementsFromSource() { XElement viewFields = new XElement("ViewFields", new XElement("FieldRef", new XAttribute("Name", "Title")), new XElement("FieldRef", new XAttribute("Name", "Body"))); SPListsService.ListsSoapClient lists = new SPListsService.ListsSoapClient(); lists.CookieContainer = App.CookieJar; lists.GetListItemsCompleted += new EventHandler<SPListsService.GetListItemsCompletedEventArgs> (lists_GetListItemsCompleted); lists.GetListItemsAsync("Maintenance Announcements", 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 Announcements SharePoint list.

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

    C#

    private void lists_GetListItemsCompleted(object sender, SPListsService.GetListItemsCompletedEventArgs e) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = store.OpenFile("Announcements.xml", FileMode.OpenOrCreate)) { e.Result.Save(stream); } } ProcessAnnouncements(e.Result); }

    The lists_GetListItemsCompleted method fires when the call to the lists.asmx SharePoint Web service completes. This method does not parse the results. This method saves the XML results to Isolated Storage. The last result set retrieved from the query are persisted on the Windows Phone 7 device. Finally, the method passes the XML results to the ProcessAnnouncements method that is responsible for parsing the results into a collection of SPAnnouncements objects. The 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.

Task 7 – Retrieving Maintenance Announcements from Isolated Storage

In this task, you will retrieve the persisted XML results from Isolated Storage. These results are stored in a collection of SPAnnouncement instances.

  1. In the WP7.SharePoint.Offline project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 10.1.4 comment to define the LoadAnnouncementsFromStorage method:

    C#

    private void LoadAnnouncementsFromStorage() { string announcementsXml = string.Empty; using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (appStorage.FileExists("Announcements.xml")) { using (StreamReader reader = new StreamReader( new IsolatedStorageFileStream("Announcements.xml", FileMode.Open, appStorage))) { announcementsXml = reader.ReadToEnd(); } } } if (!string.IsNullOrEmpty(announcementsXml)) { XElement announcements = XElement.Parse(announcementsXml); ProcessAnnouncements(announcements); } else { MessageBox.Show( @"To view announcements offline you must connect to SharePoint" + " online at least once.", "Warning", MessageBoxButton.OK); } }

    The above code retrieves the XML results from Isolated Storage. The LoadAnnouncementsFromStorage method verifies the results are not empty and then passes the results as a XElement object to the ProcessAnnouncements method, which parses the results into a collection of SPAnnouncement instances. In this lab, the method will display a message if the results are empty. This commonly happens when the device has not retrieved the results of the web service call.

Task 8 – Creating the GetAnnouncements Method in the ViewModel

In this task, you will create the GetAnnouncements method in the MainViewModel. This method is called from the view to retrieve the announcements from either the list or Isolated Storage.

  1. In the WP7.SharePoint.Offline project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 10.1.5 comment to define the GetAnnouncements method:

    C#

    public void GetAnnouncements() { if (App.IsNetworkAvailable) { Auth.Authenticate(); } else { LoadAnnouncementsFromStorage(); } }

    The GetAnnouncements method is called from the view. If the static property created in Task 5 is true, then there is network connectivity and the application will authenticate to SharePoint and retrieve the list items using the LoadAnnouncementsFromSource method after authenticating to SharePoint. If the static property is false then the application will attempt to retrieve the announcements from Isolated Storage using the LoadAnnouncementsFromStorage method.

  3. Save the MainViewModel.cs.

Task 9 – Determining Network Connectivity and Load Announcements

In this task, you will determine the network connectivity status and then load the announcements.

  1. In the WP7.SharePoint.Offline project, right-click on MainPage.Xaml and select View Code.
  2. Add the following code under the //TODO: 10.1.6 comment to define the GetAnnouncements method:

    C#

    private void GetAnnouncements() { App.IsNetworkAvailable = NetworkInterface.GetIsNetworkAvailable(); SetUIForNetworkStatus(App.IsNetworkAvailable); viewModel.GetAnnouncements(); }

    The GetAnnouncements method is called when the application starts. The method uses the NetworkInterface’s GetIsNetworkAvailable method call to determine the network connectivity status. Each time the MainPage is loaded, it checks network availability.

    Note:
    Note: The emulator does not easily support testing network connectivity and the changing of connectivity status. In this application, we simply check network status once each time the MainPage loads. In a production scenario, you can uses events to determine a change in the network connectivity and react to the change

    .

  3. Save the MainPage.xaml.cs