15
Hands-On Lab Integrating SharePoint and Windows Phone 7 using Rest APIs Lab version: 1.0.0 Last updated: 4/15/2022 Page | 1

Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

Hands-On LabIntegrating SharePoint and Windows Phone 7 using Rest APIs

Lab version: 1.0.0

Last updated: 5/18/2023

CONTENTS

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

Page | 1

Page 2: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

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

Task 2 – Creating the Maintenance Contacts 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 – Retrieving Maintenance Contacts from SharePoint...............................................................8

EXERCISE 3: TESTING THE WINDOWS PHONE 7 APPLICATION.......................................................10Task 1 – Testing the Application in the Windows Phone 7 Emulator.................................................10

SUMMARY................................................................................................................................................ 13

Page | 2

Page 3: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

Overview

Windows Phone 7 applications can use SharePoint .Net Data Services or REST-based APIs to view and edit SharePoint data. Visual Studio 2010 provides a rich development environment for creating, deploying, and integrating SharePoint list data with Windows Phone 7 applications.

Objectives

In this hands-on lab, you will learn how to use the SharePoint ListData service to display list data in a Windows Phone 7 application. This hand-on lab will only focus on reading using ListData.svc using FBA security. You will also learn how to use a SharePoint Web service to add list data to an existing list.

Learn how to use the SharePoint ListData.svc service to display list data in a Windows Phone 7 application.

Note: The ListData service supports read, write and update functionality. In this hands-on lab only read operations will be demonstratored using FBA authentication. The current supported DataSvcUtil.exe application does not support the creation of a proxy class that exposes the web request which is required to attach the FBA cookie. In this hands-on lab you will manually create a HttpWebRequest object and make an asynchronously call to the ListData.svc endpoint. Manually generating a create or update REST post is not demonstrated in this lab. Review the Security With SharePoint And Windows Phone 7 Applications module to review which scenarios support the creation and use of a service proxy.

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).

Page | 3

Page 4: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

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

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 contacts. In this scenario, the Windows Phone 7 application will read items from the maintenance contacts list and display contact details.

Task 1 – Deploying the List Template to a SharePoint Site

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

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

2. example: http://fbawp7

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

Page | 4

Page 5: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

4. Click Site Actions, and select Site Settings.

5. In the Galleries section, click List templates.

6. In the Ribbon, click the Documents tab.

7. Click Upload Document.

8. Click Browse…

9. Browse to the Maintenance Contacts.stp file located at %TrainingKit%\Labs\IntegratingDataUsingREST\Source\Before and select it.

10. Click Open.

11. Click OK.

12. Click Save.

13. Verify the Maintenance Contacts list template appears in the List Templates Gallery.

Figure 1List Template Gallery

Task 2 – Creating the Maintenance Contacts List

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

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

example: http://wp7fba

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.Page | 5

Page 6: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

5. Select the Maintenance Contacts list.

Figure 2Selecting the Maintenance Contacts template

6. In the Name textbox enter Maintenance Contacts.

7. Click Create.

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

Figure 3Maintenance Contacts list with content

Page | 6

Page 7: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to display the contacts from the maintenance contacts SharePoint list created in exercise 1. This example uses FBA. The standard method of creating a service proxy does not work with the FBA methods used in this example. In this example, we will create the http request manually, set the FBA cookie, and then manage the processing of the REST urls. The Security With SharePoint And Windows Phone 7 Applications module discusses when a service proxy can be used with SharePoint and Windows Phone 7

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.AccBas.Rest.sln file located at %TrainingKit%\Labs\IntegratingDataUsingREST\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.AccBas.Rest, 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";

Page | 7

Page 8: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

Task 3 – Retrieving Maintenance Contacts from SharePoint

In this task, you will use the SharePoint listsdata.svc endpoint to return maintenance contacts from the SharePoint list.

1. In the WP7.AccBas.Rest project, in the ViewModels folder, open the MainViewModel.cs file.

2. Add the following code under the //TODO: 4.2.1 comment to define the LoadContacts method:

C#

public void LoadContacts(){ string url = Constants.LIST_WEB_URL.TrimEnd('/') + "/_vti_bin/listdata.svc/MaintenanceContacts"; System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.CookieContainer = App.CookieJar; request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(GetContactsCallBack), request);}

The above code creates a HTTPWebRequest object, sets the CookieContainer used by FBA and then makes a call to the listdata.svc page requesting the MaintainceContacts list data. Because we are using FBA in this lab we cannot use the Visual Studio DataSvcUtil.exe to create a service proxy for the Windows Phone 7 application. The current version of the phone’s DataSvcUtil.exe does not expose the request and therefore the CookieContainer cannot be attached for authentication.

3. Add the following code under the //TODO: 4.1.2 comment to define the GetContactsCallBack method:

C#

private void GetContactsCallBack(IAsyncResult asyncResult){ HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); Stream content = response.GetResponseStream();

if (request != null && response != null) { if (response.StatusCode == HttpStatusCode.OK)

Page | 8

Page 9: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

{ using (StreamReader reader = new StreamReader(content)) { string responseString = reader.ReadToEnd(); ProcessDataFeed(responseString); reader.Close(); } } }}

The GetContactsCallBack method fires when the call to the listdata.svc service completes. The result is serialized XML which is captured in the responseString variable. The string result is passed to the ProcessDataFeed method for processing.

4. Add the following code under the //TODO: 4.1.3 comment to define the ProcessDataFeed method:

C#

private void ProcessDataFeed(string dataFeed){ XElement contacts = XElement.Load(new StringReader(dataFeed)); XNamespace baseNS = "http://www.w3.org/2005/Atom"; XNamespace dsNS = "http://schemas.microsoft.com/ado/2007/08/dataservices";

IEnumerable<SPContact> entries = from root in contacts.Descendants(baseNS + "entry") select new SPContact() { LastName = root.Descendants(dsNS + "LastName").FirstOrDefault().Value, FirstName = root.Descendants(dsNS + "FirstName").FirstOrDefault().Value, Company = root.Descendants(dsNS + "Company").FirstOrDefault().Value, Email = root.Descendants(dsNS + "EMailAddress").FirstOrDefault().Value, Address = root.Descendants(dsNS + "Address").FirstOrDefault().Value, City = root.Descendants(dsNS + "City").FirstOrDefault().Value, State = root.Descendants(dsNS + "StateProvince").FirstOrDefault().Value, Zip = root.Descendants(dsNS + "ZIPPostalCode").FirstOrDefault().Value, Phone = root.Descendants(dsNS + "BusinessPhone").FirstOrDefault().Value,

Page | 9

Page 10: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

WebSite = root.Descendants(dsNS + "WebPage").FirstOrDefault().Value, Notes = Utils.HtmlToText( root.Descendants(dsNS + "Notes").FirstOrDefault().Value), ListID = Int32.Parse(root.Descendants(dsNS + "Id").FirstOrDefault().Value) };

Deployment.Current.Dispatcher.BeginInvoke(() => { if (Contacts == null) Contacts = new ObservableCollection<SPContact>(); Contacts.Clear(); entries.ToList().ForEach(c => Contacts.Add(c)); }); }

The ProcessDataFeed method parses the result set using Linq and creates an instance of the SPContact class that represents each maintenance contact in the SharePoint list. Each SPContact object is added to the observable collection. The Contacts observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the maintenance contacts retrieved from the SharePoint list.

5. Save MainViewModel.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 in the Windows Phone 7 Emulator

In this task, you will test the Windows Phone 7 application using the Windows Phone 7 emulator.

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

2. In the WP7.AccBas.Rest project, press F5.

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

Page | 10

Page 11: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

Figure 4Application displaying contacts from the SharePoint list

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

Page | 11

Page 12: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

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

Figure 5Application displaying contact details

Page | 12

Page 13: Integrating SharePoint and Windows Phone 7 using …az12722.vo.msecnd.net/.../labs/integratingdatausingre… · Web viewWindows Phone 7 applications can use SharePoint .Net Data Services

6. Click the emulator’s back button to return to the list of contacts.

Summary

In this hands-on lab, you saw how to use the Rest APIs to integrate SharePoint data with a Windows Phone 7 application. You learned how to use the ListsData service with FBA authentication and retrieve list data from a Windows Phone 7 application.

Page | 13