integrating sharepoint and windows phone 7 in...

21
Hands-On Lab Integrating SharePoint and Windows Phone 7 in Offline Mode Lab version: 1.0.0 Last updated: 7/7/2022 Page | 1

Upload: others

Post on 03-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

Hands-On LabIntegrating SharePoint and Windows Phone 7 in Offline Mode

Lab version: 1.0.0

Last updated: 5/19/2023

Page | 1

Page 2: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

CONTENTS

OVERVIEW................................................................................................................................................. 3

EXERCISE 1: CREATING A SHAREPOINT LIST DATA SOURCE...........................................................4Task 1 – Deploying the List Template to a SharePoint Site..................................................................4

Task 2 – Creating the Maintenance Announcement List.....................................................................5

EXERCISE 2: CREATING THE WINDOWS PHONE 7 APPLICATION.....................................................7Task 1 – Beginning the Exercise...........................................................................................................7

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

Task 3 – Adding a Reference to the SharePoint Lists.asmx Web Service.............................................7

Task 4 – Modifying the ServiceReferences.ClientConfig File to Support the Cookie Container Used With Forms Based Authentication.......................................................................................................8

Task 5 – Creating a Static Property to Store Network Connectivity Status..........................................9

Task 6 – Retrieving and Storing Maintenance Announcements from SharePoint...............................9

Task 7 – Retrieving Maintenance Announcements from Isolated Storage........................................11

Task 8 – Creating the GetAnnouncements Method in the ViewModel.............................................12

Task 9 – Determining Network Connectivity and Load Announcements...........................................12

EXERCISE 3: TESTING THE WINDOWS PHONE 7 APPLICATION.......................................................13Task 1 – Testing the Application’s Online Functionality.....................................................................13

Task 2 - Testing the Application’s Offline View Functionality.............................................................15

SUMMARY................................................................................................................................................ 18

Page | 2

Page 3: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

Overview

Windows Phone 7 applications require network connectivity to work with SharePoint data. Network connectivity is not always available. Windows Phone 7 can use Isolated Storage to persist data on the phone for offline access. Windows Phone 7 applications that require offline access to SharePoint data can work in offline mode when a network connection is not available and work in a connected mode when a network connection is available.

Objectives

In this hands-on lab, you will learn how to dertermine if a network connection is avaiable on aWindows Phone 7 mobile device. You will also learn how to store and retrieve list data from Isolated Storage.

Learn how to determine network connectivity status.

Learn how to store and retrieve list data from Isolated Storage for offline use.

Learn how to test offline functionality in the Windows Phone 7 emulator.

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

Page | 3

Page 4: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

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

Exercise 1: Creating a SharePoint List Data Source

In this exercise, you will deploy a list template to SharePoint and make a list based upon the template. The list template defines a list used to display important maintenance announcements. In this scenario, the Windows Phone 7 application will maintain an offline version of the list data. This will allow current uesrs to read announcements when the mobile device does not have network connectivity.

Task 1 – Deploying the List Template to a SharePoint Site

In this task, you will deploy the maintenance announcements list template to a SharePoint site. The list template contains sample data used in the lab.

Note: This lab uses the same Maintenance Announcements list as Lab 4.1. If you have already created a Maintenance Announcement list in Lab 4.1 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 Site Settings.

4. In the Galleries section, click List templates.

Page | 4

Page 5: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

5. In the Ribbon, click the Documents tab.

6. Click Upload Document.

7. Click Browse…

8. Browse to the Maintenance Announcements.stp file located at %TrainingKitPath%\Labs\Tips\Offline\Source\Before and select it.

9. Click Open.

10. Click OK.

11. Click Save.Verify the Maintenance Announcements list template appears in the List Templates Gallery.

Figure 1Maintenance Announcements in the List Template gallery

Task 2 – Creating the Maintenance Announcement List

In this task, you will use the maintenance announcements list template to create the maintenance announcements list.

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 Maintenance Announcements list.

Page | 5

Page 6: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

Figure 2Create List Dialog

6. In the Name textbox enter Maintenance Announcements.

7. Click Create.

8. Verify the Maintenance Announcements list contains the following sample data.

Figure 3The new Maintenance Announcements list

Page | 6

Page 7: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

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.

a. Browse to the WP7.SharePoint.Offline.sln file located at %TrainingKitPath%\Labs\Tips\Offline\Source\Before and select it.

b. 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 = "http://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.

Page | 7

Page 8: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

1. In the Solution Explorer, in the WP7.SharePoint.Offline 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 Announcements list.

Example: http://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 – Modifying the ServiceReferences.ClientConfig File to Support the Cookie Container Used With Forms Based Authentication

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 4Add Cookie Support to the Binding element

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

Page | 8

Page 9: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

Figure 5Error 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 SharePointPage | 9

Page 10: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

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);}

Page | 10

Page 11: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

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(

Page | 11

Page 12: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

@"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 AnnouncementsPage | 12

Page 13: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

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: 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

Exercise 3: Testing the Windows Phone 7 Application

In this exercise, you will test the Windows Phone 7 application.

Task 1 – Testing the Application’s Online Functionality

In this task, you will test the Windows Phone 7 application using the Windows Phone 7 emulator in an online mode.

1. In the WP7.SharePoint.Offline solution, select Windows Phone 7 Emulator in the deployment location dropdown list

2. In the WP7.SharePoint.Offline solution, press F5.

Page | 13

Page 14: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

3. The Windows Phone application starts in the emulator and displays the items from the Maintenance Announcements SharePoint list.

Figure 6Items from the Maintenance Announcements list

Page | 14

Page 15: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

4. Click on one of the maintenance announcements in the list to select it.

5. The Windows Phone application displays the details for the selected maintenance announcement from the SharePoint list.

6. Do not shut down the emulator.

7. In Visual Studio, click the Stop Debugging button or press Shift + F5.

Note: Shutting down the emulator clears Isolated Storage. Stopping the debugger does not clear Isolated Storage.

Task 2 - Testing the Application’s Offline View Functionality

In this task, you will test the offline viewing capability of the Windows Phone 7 application by removing the emulator’s network capability.

1. In the WP7.SharePoint.Offline project, in the Properties folder, open the WMAppManifest.xml file.

2. Locate the ID_CAP_NETWORKING Capability node.

Figure 7Networking capability node

3. Highlight the ID_CAP_NETWORKING node.

4. Click the comment button located on the Visual Studio toolbars.

Figure 8Visual Studio comment button

Page | 15

Page 16: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

5. The ID_CAP_NETWORKING capability node should look like this:

Figure 9Networking capability line disabled with a comment

6. Save the WMAppManifest.xml.

7. In the WP7.SharePoint.Offline solution, press F5.

8. The Windows Phone application starts in the emulator and displays the items from the Maintenance Announcements SharePoint list.

Page | 16

Page 17: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

Figure 10Windows Phone with disabled capability based on the offline status

Notice that the page title is displayed in red and it indicates the application is offline. Removing the Network capabilities for the emulator placed the application in an offline state and the

Page | 17

Page 18: Integrating SharePoint and Windows Phone 7 in …az12722.vo.msecnd.net/sharepointandwindowsphone7… · Web viewWindows Phone 7 applications require network connectivity to work with

application retrieved the list data from Isolated Storage. Notice that the add icon is disabled. In this lab, we do not manage replication or synchronization.

Summary

In this hands-on lab, you saw how to determine network connectivity status for Windows Phone 7 devices. You also learned how to use Isolated Storage to save list data results for offline use. This requires the application to check for network connectivity and change the application flow when the application is offline. You also learned that the emulator does not support offline testing easily. True offline testing will happen on an actual device

Page | 18