Hands-On Lab

Integrating SharePoint and Windows Phone 7 Tile Notifications

Lab version: 1.0.0

Last updated: 7/14/2011

Contents

Overview 3

Exercise 1: Creating a SharePoint List Data Source 4

Task 1 – Creating the Maintenance Training Calendar List 4

Exercise 2: Complete the Notification Registration WCF Service 5

Task 1 – Beginning the Exercise 6

Task 2 – Adding a Count Property to the ListNotificationRegistration Class 6

Task 3 – Declaring Service Methods to Manage the Count 6

Task 4 – Implementing the New Service Methods 7

Task 5 – Implementing the Method to Send a Tile Notification with a Zero Count 8

Task 6 – Running the Service in Visual Studio 2010 9

Exercise 3: Creating the List Event Receiver to Send Notifications 11

Task 1 – Adding the EventNotification Project to the Solution 11

Task 2 – Completing the Notification.cs Class to Send Tile Notifications 11

Task 3 – Adding a Reference to the NotificationRegistration WCF Service 13

Task 4 – Verifying the Event Receiver Builds and Deploys Correctly. 14

Exercise 4: Creating the Windows Phone 7 Application 16

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

Task 2 – Configuring Constants in the Windows Phone 7 Application 17

Task 3 – Completing the SettingsViewModel.cs Class 18

Task 4 – Completing the MainPage.xaml.cs File 19

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

Task 6 – Adding a Reference to the NotificationRegistration WCF Service 20

Task 7 – Modifying the ServiceReferences.ClientConfig File to Support the Cookie Container Used with Forms Based Authentication 21

Exercise 5: Testing the Windows Phone 7 Application 22

Task 1 – Testing the Application’s View Functionality in Windows Phone 7 Emulator 23

Task 2 -Testing the Toast Notification in Windows Phone 7 Emulator 24

Summary 35

Overview

Windows Phone 7 applications can use Windows Push Notification Services to notify users of custom events. Windows Phone 7 applications integrating with SharePoint can use notifications to alert users of changes in SharePoint data sources. Tile notifications can display a count of events such as the number of changes to a SharePoint list.

Objectives

In this hands-on lab, you will learn how to create a list event handler that will send tile notifications with count information. You will learn how to to register for notifications and bind the phone and application to a tile notification. You will learn how to manage counts for each subsciber.

·  Learn how to use a SharePoint list event reciever to create tile notifications with count information.

·  Learn how register a device with a custom WCF service to subscribe to notifications.

·  Learn how to manage event counts per registration.

Prerequisites

The following is required to complete this hands-on lab:

Note: See Setting Up A SharePoint and Windows Phone 7 Development Environment Module for instructions that describe how to set up the SharePoint and Windows Phone 7 developer machine.

·  Windows 7 x64 installed with all Windows Updates installed, in one of the following scenarios.

◦  Installed on a physical machine

◦  Installed on a bootable VHD

·  SharePoint 2010 installed on the Windows 7 x64 developer machine configured with a site collection that uses Forms Based Authentication (FBA).

·  Windows Phone 7 Developer Tools

◦  http://download.microsoft.com/download/1/7/7/177D6AF8-17FA-40E7-AB53-00B7CED31729/vm_web.exe

·  Windows Phone 7 Developer Tools - January 2011 Update

◦  http://download.microsoft.com/download/6/D/6/6D66958D-891B-4C0E-BC32-2DFC41917B11/WindowsPhoneDeveloperResources_en-US_Patch1.msp

·  Windows Phone Developer Tools Fix

◦  http://download.microsoft.com/download/6/D/6/6D66958D-891B-4C0E-BC32-2DFC41917B11/VS10-KB2486994-x86.exe

Note: The following prerequisites are not included in the Setting Up A SharePoint and Windows Phone 7 Development Environment Module installation instructions. If you are using a development machine built according to the Setting Up A SharePoint and Windows Phone 7 Development Environment Module instructions you must install these components.

·  KB981002- WCF: Hotfix rollup in .NET 3.5 SP1 for Win 7 and Win 2k8 R2

◦  http://code.msdn.microsoft.com/KB981002

·  Internet connectivity

Note: This lab requires the firewall be turned off to enable connection to the Push Notification Service.

Exercise 1: Creating a SharePoint List Data Source

In this exercise, you will create a new calendar list that will contain training events. In this scenario, the Windows Phone 7 application will read the calendar and display the events stored in it. The Windows Phone 7 application allows users to receive Toast notifications when items in the list are added, modified or deleted. Users with write permissions will be able to add events to the list to test the application.

Task 1 – Creating the Maintenance Training Calendar List

In this task, you will use the calendar list template to create the maintenance training schedule list.

Note: If you have created the list from Integrating SharePoint and Windows Phone 7 Notifications Lab of the Integrating Push Notifications with SharePoint Data in Windows Phone 7 Applications Module then you can reuse the Maintenance Training Scheduling list from Lab 1 and skip to Exercise 2.

1.  Open Internet Explorer and navigate to the SharePoint Team Site configured for Forms Based Authentication.

example: http://fbawp7

2.  Log into the site using site collection administrator credentials.

3.  Click Site Actions and select More Options.

4.  In the Filter By section, select List.

5.  Select the Calendar list.

Figure 1

Selecting a Calendar list template.

6.  In the Name textbox, enter Maintenance Training Schedule.

7.  Click Create.

Exercise 2: Complete the Notification Registration WCF Service

In this exercise, you will add methods to the existing registration WCF service to manage the notification count for each registration.

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.

3.  Browse to the WP7.Notification.Events.sln file located at %TrainingKitPath%\Labs\IntegratingTileNotifications\Source\Before and select it.

4.  Click Open to open the solution.

Task 2 – Adding a Count Property to the ListNotificationRegistration Class

In this task, you will add an integer property to the existing ListNotificationRegistration class.

1.  In the NotificationRegistration project, open the ListNotificationRegistration.cs file located in the Model folder.

2.  Add the following code under the //TODO: 7.2.1 comment to define the CurrentCount property:

C#

int count;

public int CurrentCount

{

get { return count; }

set { count = value; }

}

The above property is used to maintain the current count of notifications that have been sent since the last time the phone application has cleared the count.

3.  Save and close the ListNotificationRegistration.cs file.

Task 3 – Declaring Service Methods to Manage the Count

In this task, you will complete the existing IListNotificationChannels.cs interface to include the required methods declarations to manage the notification count for each registrations.

1.  In the NotificationRegistration project, open the IListNotificationChannels.cs file.

2.  Add the following code under the //TODO: 7.2.2 comment to define the following two method declarations:

C#

[OperationContract]

void ClearRegistrationCount(string channelUri, string listId);

[OperationContract]

List<ListNotificationRegistration>

RetrieveChannelURIsForListIdWithCountIncrement(string listId);

The above code defines the two methods used to clear a registration count and increment a registration count. The OperationContract attribute decorates these methods to expose the methods as part of the public interface.

Task 4 – Implementing the New Service Methods

In this task, you will complete the existing ListNotificationChannels class to include the declared interface methods from Task 3.

1.  In the NotificationRegistration project, open the ListNotificationChannels.svc file.

2.  Add the following code under the //TODO: 7.2.3 comment to define the RetrieveChannelURIsForListIdWithCountIncrement method:

C#

public List<ListNotificationRegistration>

RetrieveChannelURIsForListIdWithCountIncrement(string listId)

{

var items = from r in ListNotificationRegistrations

where r.ListId.ToLower() == listId.ToLower()

select r;

List<ListNotificationRegistration> notifications = items.ToList();

notifications.ForEach(n => n.CurrentCount++);

return notifications;

}

The above code uses Linq to retrieve the registrations for the specific list id. It then increments each registration by one and returns the ListNotificationRegistration collection. The event receiver will call this method for each event managed by the event receiver.

3.  Add the following code under the //TODO: 7.2.4 comment to define the ClearRegistrationCount method:

C#

public void ClearRegistrationCount(string channelUri, string listId)

{

var reg = (from r in ListNotificationRegistrations

where r.ChannelUri.ToLower() == channelUri.ToLower() &

r.ListId.ToLower() == listId.ToLower()

select r).SingleOrDefault<ListNotificationRegistration>();

if (reg != null)

{

reg.CurrentCount = 0;

Notification.SendNewTile(channelUri);

}

}

The above code uses Linq to retrieve a single ListNotificationRegistration object for the specific channel Uri and list id. If a matching ListNotificationRegistration is found the CurrentCount is set to zero and the service will send a new tile notification with a zero count. A zero count will remove the counter from the tile located on the phone. This method is called by the phone application each time the application starts.

Task 5 – Implementing the Method to Send a Tile Notification with a Zero Count

In this task, you will complete the existing Notification class to send a tile notification to a specific channel Uri with a zero count.

1.  In the NotificationRegistration project, open the Notification.cs file.

2.  Add the following code under the //TODO: 7.2.5 comment to define the SendNewTile method:

C#

public static void SendNewTile(string channelUri)

{

string tilePushXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<wp:Notification xmlns:wp=\"WPNotification\">" +

"<wp:Tile>" +

"<wp:Count>0</wp:Count>" +

"</wp:Tile> " +

"</wp:Notification>";

HttpWebRequest sendNotificationRequest =

(HttpWebRequest)WebRequest.Create(channelUri);

sendNotificationRequest.Method = "POST";

sendNotificationRequest.Headers = new WebHeaderCollection();

sendNotificationRequest.ContentType = "text/xml";

sendNotificationRequest.Headers.Add("X-NotificationClass", "1");

sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");

byte[] strBytes = new UTF8Encoding().GetBytes(tilePushXml);

sendNotificationRequest.ContentLength = strBytes.Length;

using (Stream requestStream = sendNotificationRequest.GetRequestStream())

{

requestStream.Write(strBytes, 0, strBytes.Length);

}

try

{

var response = (HttpWebResponse)sendNotificationRequest.GetResponse();

var notiticationStatus = response.Headers["X-NotificationStatus"];

var notitificationChannelStatus =

response.Headers["X-SubscriptionStatus"];

var deviceConnectionStatus =

response.Headers["X-DeviceConnectionStatus"];

}

catch

{

// Ignoring the response.

// In a production application you should review the response and

// code appropriate for the specific response.

// http://msdn.microsoft.com/en-us/library/ff941100%28v=VS.92%29.aspx

}

}

The above code creates an HttpWebRequest object and posts XML to the channel uri provided to the method. The XML defines the type of notification as well as the details of the notification. A Tile notification can set the count, background image and the text of the title. This method sends the static count of zero to the phone. A count of zero will remove the counter from the tile.

The notification is sent using the channel Uri. It is sent to the Push Notification Service which sends the message to the device using the connection between the device and Push Notification Service. This notification will clear the count from the application tile.

Task 6 – Running the Service in Visual Studio 2010

In this task, you will run the NotificationRegistration service to verify it starts correctly.

1.  In the Solution Explorer, in the NotificationRegistration project, right click ListNotificationChannels.svc and select Set as Start Page.

2.  Press F5 to run the service. The service should start in the WCF Test Harness or display the Service page in the web browser.

Figure 2

NotificationRegistration WCF Test Client

Figure 3

NotificationRegistration WCF Services in Internet Explorer

3.  In Visual Studio, press Shift + F5 to stop debugging.

Exercise 3: Creating the List Event Receiver to Send Notifications

In this exercise, you will create the SharePoint list event receiver to send tile notifications with a notification count to registered devices. The SharePoint list event receiver will be associated with calendar lists.

Task 1 – Adding the EventNotification Project to the Solution

In this task, you add the existing EventNotification project to the solution.

1.  In the Solution Explorer, right-click the WP7.Notification.Events solution, and select Add | Existing Project.

2.  Browse to EventNotification.csproj located in the EventNotification folder and select it.

3.  Click Open.

Figure 4

Solution Explorer with WCF Service project and Event Receiver project

Task 2 – Completing the Notification.cs Class to Send Tile Notifications

In this task, you will complete the existing Notification class to send a toast notification to any devices registered for notification.

1.  In the EventNotification project, open the Notification.cs file.

2.  Add the following code under the //TODO: 7.2.6 comment to define the SendNotification method:

C#

public static void SendNotification(string channelUri, int count)

{

string tilePushXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<wp:Notification xmlns:wp=\"WPNotification\">" +

"<wp:Tile>" +

"<wp:Count>{0}</wp:Count>" +

"</wp:Tile> " +

"</wp:Notification>";

string tilePushXmlFormatted = string.Format(tilePushXml, count);

HttpWebRequest sendNotificationRequest =

(HttpWebRequest)WebRequest.Create(channelUri);

sendNotificationRequest.Method = "POST";

sendNotificationRequest.Headers = new WebHeaderCollection();

sendNotificationRequest.ContentType = "text/xml";

sendNotificationRequest.Headers.Add("X-NotificationClass", "1");

sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");

byte[] strBytes = new UTF8Encoding().GetBytes(tilePushXmlFormatted);

sendNotificationRequest.ContentLength = strBytes.Length;

using (Stream requestStream = sendNotificationRequest.GetRequestStream())

{

requestStream.Write(strBytes, 0, strBytes.Length);

}

try

{

var response = (HttpWebResponse)sendNotificationRequest.GetResponse();

var notiticationStatus = response.Headers["X-NotificationStatus"];

var notitificationChannelStatus =

response.Headers["X-SubscriptionStatus"];

var deviceConnectionStatus =

response.Headers["X-DeviceConnectionStatus"];

}

catch

{

// Ignoring the response.

// In a production application you should review the response and

// code appropriate for the specific response.

// http://msdn.microsoft.com/en-us/library/ff941100%28v=VS.92%29.aspx

}

}

The above code creates a HttpWebRequest object and posts XML to the channel Uri provided to the method. The XML defines the type of notification as well as the details of the notification. A Tile notification can set the count, background image and the text of the title. This method will send the phone a count value based on the passed in parameter value. This method is similar to the SendNotification method located in the WCF registration service. The difference is that this method adds the current count to the notification instead of sending a default count of 0.

The Xml payload is sent to the Push Notification Service which then send the notification to the device using the channel between the device and the Push Notification Service.

Task 3 – 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 EventNotification project, right click Service References and select Add Service Reference.

2.  Click Advanced…

3.  Click Add Web Reference…

4.  Click Web services in this solution

Figure 5

Selecting Web Services in this solution