Exercise 4: 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. The application will allow users to opt in for notifications. The application will enable and disable tile notification based on the setting. The tile notification will include a count value.

Task 1 – Adding the WP7.Notification.Events.PhoneApp Project to the Solution

In this task, you add the existing WP7.Notification.Events.PhoneApp project to the solution.

  1. In the Solution Explorer, right-click the WP7.Notification.Events solution, and select Add | Existing Project.
  2. Browse to WP7.Notification.Events.PhoneApp.csproj located in the PhoneApp folder.
  3. Click Open.

    Figure 10

    Solution Explorer with three projects displayed

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.Notification.Events.PhoneApp 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";

  5. Change the LIST_TITLE constant to Maintenance Training Schedule.
  6. Change the LIST_ID constant to the list id of the Maintenance Training Schedule.
    1. Open the Maintenance Training Schedule list in Internet Explorer.
    2. Select the Calendar Ribbon tab.
    3. Click List Settings Ribbon button.
    4. Select the value of the List URL parameter from the browser’s address bar.
    5. The value of the list id in the address bar is URL encoded. Remove the %7B and %7D values at the beginning and end of the parameter. Replace %2D with the dash character.

      For example:

      %7B618FD6BA%2DE587%2D4C93%2D9143%2DD21B2C01E8AD%7D

      Is converted to:

      618FD6BA-E587-4C93-9143-D21B2C01E8AD

      C#

      public const string LIST_ID = "618FD6BA-E587-4C93-9143-D21B2C01E8AD";

      Note:
      Note: If the list id constant is not correct, the event handler will not receive the channel Uri (device) from the registration service. Copy the unencoded list id to the LIST_ID constant value.

      Note:
      Note: The above code is an example. You must use your unique list id.

Task 3 – Completing the SettingsViewModel.cs Class

In this task, you will complete the existing SettingsViewModel class. This class manages the application’s connection and bindings for notification.

  1. In the Solution Explorer, open the SettingsViewModel.cs file located in the ViewModels folder.
  2. Add the following code under the //TODO: 7.2.7 comment to define the UpdateNotificationBindings method:

    C#

    private void UpdateNotificationBindings() { if (IsNotificationEnabled) { if (Channel != null && !Channel.IsShellTileBound) { Channel.BindToShellTile(); } } else { if (Channel.IsShellToastBound) { Channel.UnbindToShellTile(); } } }

    The above code will bind or unbind the device to the tile notifications. When bound the phone will display the new tile as it receives the notification from the Push Notification Service. When the user opts to not receive notifications the channel is not bound to the notification. The phone will not change the application tile.

  3. Add the following code under the //TODO: 7.2.8 comment to define the ClearTileCount method.

    C#

    public void ClearTileCount() { var svc = new NotificationRegistration.ListNotificationChannelsClient(); svc.ClearRegistrationCountAsync(channelUri.ToString(), Constants.LIST_ID); }

    The above code calls the Notification Registration service to clear the count value associated with the specific channel Uri and list id. This methond is called when the application starts. This method calls the registration service’s ClearTileCount. The ClearTileCount will set the count to zero for the channel and list id combination and send a tile notification with a 0 count. Each time the application starts the tile count will return to zero which will display no count on the application tile.

Task 4 – Completing the MainPage.xaml.cs File

In this task, you will complete the MainPage.xaml.cs file.

  1. In the Solution Explorer, in the WP7.Notification.Events.PhoneApp project, right click MainPage.xaml and select View Code.
  2. Add the following code under the //TODO: 7.2.9 comment to define the MainPage constructor method.

    C#

    public MainPage() { InitializeComponent(); viewModel = new MainViewModel(); DataContext = viewModel; viewModel.GetEvents(); App.settingsViewModel = new SettingsViewModel(); var settings = SettingsViewModel.Current; if (settings.IsNotificationEnabled) { settings.EnableNotifications(); settings.ClearTileCount(); } }

    The above constructor code sets the viewModel and DataContext for the main page. The code creates the single SettingsViewModel object and determines if notifications are enabled. If notifications are enabled it calls the EnableNotifications method of the SettingsViewModel to create and configure the notification channel. It then calls the ClearTileCount method. This will clear the tile counter each time the application is opened.

  3. Save and close MainPage.Xaml.cs.

Task 5 – 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.Notification.Events.PhoneApp 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 Training Schedule list.
  3. Example: https://fbawp7/_vti_bin/lists.asmx
  4. Click Go.
  5. Once the service is resolved, enter ListSvc in the Namespace textbox.
  6. Click OK.

Task 6 – Adding a Reference to the NotificationRegistration WCF Service

In this task, you will add a reference to the NotificationRegistration WCF service.

  1. In the Solution Explorer, in the WP7.Notification.Events.PhoneApp project, right click Service References and select Add Service Reference.
  2. Click Discover.
  3. After the ListNotificationChannelsSvc service is discovered, enter NotificationRegistration in the Namespace textbox.
  4. 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 Module 6 for more information about Forms Based Authentication.

  1. In the WP7.Notification.Events.PhoneApp 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 11

    Adding the enableHttpCookieContainer in the ServiceReferences.ClientConfig file

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

    Figure 12

    Error message received when editing a service with the enableHttpCookieContainer attribute

    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.