Integrating Images Stored in Sharepoint with Windows Phone 7 Using Web Services

Total Page:16

File Type:pdf, Size:1020Kb

Integrating Images Stored in Sharepoint with Windows Phone 7 Using Web Services

1.

2.

Hands-On Lab Integrating Images Stored in SharePoint with Windows Phone 7 using Web Services

Lab version: 1.0.0 Last updated: 4/7/2018

3. 4.

Page | 1 5.

6.

Page | 2 CONTENTS 7.

Page | 3 8. Overview

9. 10. Windows Phone 7 applications can use SharePoint Web services to view images stored in a library. Visual Studio 2010 provides a rich development environment for creating, deploying, and integrating SharePoint Asset libraries with Windows Phone 7 applications.

Objectives 1. In this hands-on lab, you will learn how to use a SharePoint Web service to display images stored in a SharePoint library in a Windows Phone 7 application. You will also learn how to use a converter class to retrieve an image from a URL and bind to an Image object in a Windows Phone 7 application. Learn how to use the SharePoint lists.asmx Web service to retrieve Asset library data from a Windows Phone 7 application. Learn how to use a converter object in a Windows Phone 7 application to retrieve images from a URL and bind to an Image object.

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

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

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

Page | 4 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

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

5. 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 Asset Library Data Source

1. In this exercise, you will deploy a list template to SharePoint and make an Asset library based upon the list template. The list template defines an Asset library that contains vehicle images. In this scenario, the Windows Phone 7 application will read from the Asset library and display the images. Task 1 – Deploying the List Template to a SharePoint Site In this task, you will deploy the vehicle images list template to a SharePoint site. The list template contains sample data used in the lab. 2. Open Internet Explorer and navigate to the SharePoint Team Site configured for Forms Based Authentication. 3. example: http://fbawp7 4. Log into the site using site collection administrator credentials. 5. Click Site Actions, and select Site Settings. 6. In the Galleries section, click List templates.

Page | 5 7. In the Ribbon, click the Documents tab. 8. Click Upload Document. 9. Click Browse… 10. Browse to the Vehicle Images.stp file located at %TrainingKitPath %\Labs\IntegratingSharePointImages\Source\Before and select it. 11. Click Open. 12. Click OK. 13. Click Save.

14. Verify the Vehicle Images list template appears in the List Templates Gallery.

14.a.

14.b. Figure 1 14.c. List Template Gallery

14.d.

Task 2 – Creating the Vehicle Image Library 1. In this task, you will use the vehicle images library template to create the vehicle images library. 1. Open Internet Explorer and navigate to the SharePoint Team Site configured for Forms Based Authentication. 1.a. example: http://fbawp7 2. Log into the site using site collection administrator credentials.

Page | 6 3. Click Site Actions and select More Options. 4. In the Filter By section, select Library. 5. Select the Vehicle Images library.

1.b. 1.c. Figure 2 1.d. Selecting the Vehicle Images template

1.e. 6. In the Name textbox enter Vehicle Images. 7. Click Create. 8. Verify the Vehicle Images library contains the following sample images.

1.f. 1.g. Figure 3 1.h. Vehicle Images list with content

Page | 7 1.i.

Exercise 2: Creating the Windows Phone 7 Application

1. In this exercise, you will create a Windows Phone 7 application to read and display images from the vehicle images SharePoint library created in exercise 1. Task 1 – Beginning the Exercise 2. 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. 2.a. Browse to the WP7.AccAdv.Images.sln file located at %TrainingKitPath %\Labs\IntegratingSharePointImages\Source\Before and select it. 2.b. Click Open to open the solution.

Task 2 – Configuring Constants in the Windows Phone 7 Application 3. 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.AccAdv.Images 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. 3.a. C# 3.b. public const string AUTHENTICATION_SERVICE_URL = "http://fbawp7/_vti_bin/authentication.asmx";

3.c.

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

Page | 8 1. In this task, you will add a reference to the SharePoint lists.asmx Web service. 1. In the Solution Explorer, in the WP7.AccAdv.Images project, right click Service References and select Add Service Reference. 1. In the Address textbox enter the URL to the lists.asmx SharePoint web service for the site where you created the Vehicle Images library. Example: http://fbawp7/_vti_bin/lists.asmx 2. Click Go. 3. Once the service is resolved, enter ListService in the Namespace textbox. 4. Click OK.

Task 4 – Modifying the ServiceReferences.ClientConfig File to Support the Cookie Container Used With Forms Based Authentication 2. 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.AccAdv.Images project, open the ServiceReferences.ClientConfig file. 2. Locate the ListsSoap binding element. 3. Add the following attribute to the ListsSoap binding element. 2.a. XML 2.b. enableHttpCookieContainer="true"

2.c. The following screenshot shows what the ListSoap binding element looks like after the above code is added.

2.d.

2.e. Figure 4 2.f. Adding the emableHTttpCookieContainer attribute

2.g.

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

2.i.

2.j.

2.k. Figure 5 2.l. Expected exception when modifying a service with the enabledHttpCookieContainer defined

2.m.

2.n. 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.

2.o.

Task 5 – Retrieving Vehicle Images from SharePoint 1. In this task, you will use the SharePoint lists.asmx Web service to return vehicle images from the SharePoint library.

Page | 10 1. In the WP7.AccAdv.Images project, in the ViewModels folder, open the MainViewModel.cs file. 2. Add the following code under the //TODO: 5.1.1 comment to define the LoadImages method: 2.a. C# 2.b. public void LoadImages() 2.c. { 2.d. XElement viewFields = new XElement("ViewFields", 2.e. new XElement("FieldRef", 2.f. new XAttribute("Name", "Title")), 2.g. new XElement("FieldRef", 2.h. new XAttribute("Name", "_Comments")), 2.i. new XElement("FieldRef", 2.j. new XAttribute("Name", "ThumbnailOnForm"))); 2.k. 2.l. ListsSoapClient lists = new ListsSoapClient(); 2.m. lists.CookieContainer = App.CookieJar; 2.n. lists.GetListItemsCompleted += new EventHandler 2.o. (lists_GetListItemsCompleted); 2.p. lists.GetListItemsAsync("Vehicle Images", string.Empty, null, viewFields, 2.q. null, null, null); 2.r. }

2.s. 2.t. The above code uses the proxy class Visual Studio 2010 generated for the lists.asmx service to query the Vehicle Images SharePoint library. 3. Add the following code under the //TODO: 5.1.2 comment to define the lists_GetListItemsCompleted method: 3.a. C# 3.b. void lists_GetListItemsCompleted(object sender, GetListItemsCompletedEventArgs e) 3.c. { 3.d. IEnumerable rows = e.Result.Descendants(XName.Get 3.e. ("row", "#RowsetSchema"));

Page | 11 3.f. IEnumerable images = from element in rows 3.g. select new SPImage() 3.h. { 3.i. Title = (string)element.Attribute("ows_Title"), 3.j. Comments = Utils.HtmlToText((string) 3.k. element.Attribute("ows__Comments")), 3.l. ImgUrl = (string)element.Attribute 3.m. ("ows_ThumbnailOnForm") 3.n. }; 3.o. 3.p. Deployment.Current.Dispatcher.BeginInvoke(() => 3.q. { 3.r. if (Images == null) 3.s. { 3.t. Images = new ObservableCollection(); 3.u. } 3.v. Images.Clear(); 3.w. images.ToList().ForEach(a => Images.Add(a)); 3.x. }); 3.y. }

3.z. 3.aa. The lists_GetListItemsCompleted method fires when the call to the lists.asmx SharePoint Web service completes. The method parses the result set, creates an instance of the SPImage class that represents each image in the SharePoint library, and adds the SPImage instances to the Images observable collection. The Images observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the vehicle images retrieved from the SharePoint library. 4. Save MainViewModel.cs.

Task 6 – Binding Image URL to an Image Object Using a Custom Converter 1. In this task, you will use a custom converter object to retrieve an image from a URL and bind to an Image object in the MainPage user control. 1. In the WP7.AccAdv.Images project, in the Utilities folder, open the Utils.cs file. 2. Add the following code under the //TODO: 5.1.3 comment to define the Convert method:

Page | 12 2.a. C# 2.b. public object Convert(object value, Type targetType, object parameter, 2.c. CultureInfo culture) 2.d. { 2.e. BitmapImage bmp=new BitmapImage(); 2.f. 2.g. System.Net.HttpWebRequest request = 2.h. (HttpWebRequest)HttpWebRequest.Create(value.ToString()); 2.i. request.CookieContainer = App.CookieJar; 2.j. request.Method = "GET"; 2.k. request.BeginGetResponse((s) => 2.l. { 2.m. HttpWebRequest requestResult = (HttpWebRequest)s.AsyncState; 2.n. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(s); 2.o. Stream content = response.GetResponseStream(); 2.p. System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 2.q. { 2.r. bmp.SetSource(content); 2.s. }); 2.t. }, request); 2.u. 2.v. return bmp; 2.w. }

2.x. 2.y. The ImageConverter class’s Convert method is used in the MainPage user control to retrieve the image binary using the image url. The Convert method binds the image stream to a BitmapImage object. Image controls contained in the MainPage user control pass the Image URL to the converter and bind to the BitmapImage object returned by the Convert method. 3. Save the Utils.cs. 4. In the WP7.AccAdv.Images project, open the MainPage.xaml file. 5. Replace the comment with the following namespace markup:

Page | 13 5.a. Xaml 5.b. xmlns:local="clr- namespace:WP7.AccAdv.Images;assembly=WP7.AccAdv.Images"

5.c. Note: You must replace the TODO comment. The project will not run correctly if the TODO comment is not removed.

5.d. 5.e. The namespace markup associates a prefix (local) with a namespace (WP7.AccAdv.Images) from an assembly. The compiler uses the namespace to identify a resource or class. 6. Add the following markup under the comment to define the ImageConverter as a resource : 6.a. Xaml 6.b. 6.c. 6.d.

6.e. 6.f. The PhoneApplicationPage.Resouces markup creates a resource library scoped at the page level. This resource library contains an instance of the ImageConverter object. It is be accessed using the ImageConverter key. 7. Replace the comment with the following markup:

7.a. Xaml 7.b. Converter={StaticResource ImageConverter}

7.c.

7.d. Example:

7.g. 7.h. This markup instructs the code to call the ImageConverter class to convert the data bound value. Image objects cannot use a URL as a source. The ImageConverter object is used to convert the URL reference to a BitmapImage object which can be used as a Source object.

7.i. Note: You must replace the TODO comment. The project will not run correctly if the TODO comment is not removed.

Page | 14 7.j. 8. Replace the comment with the following markup:

8.a. Xaml 8.b. Converter={StaticResource ImageConverter}

8.c.

8.d. Example:

8.g. 8.h. This markup instructs the code to call the ImageConverter class to convert the data bound value. Image objects cannot use a URL as a source. The ImageConverter object is used to convert the URL reference to a BitmapImage object which can be used as a Source object.

8.i. Note: You must replace the TODO comment. The project will not run correctly if the TODO comment is not removed.

Exercise 3: Testing the Windows Phone 7 Application

1. In this exercise, you will test the Windows Phone 7 application. Task 1 – Testing the Application’s View Functionality in Windows Phone 7 Emulator 2. In this task, you will test the Windows Phone 7 application using the Windows Phone 7 emulator. 1. In the WP7.AccAdv.Images solution, select Windows Phone 7 Emulator in the deployment location dropdown list. 2. In the WP7.AccAdv.Images solution, press F5. 4. The Windows Phone application starts in the emulator and displays the images from the Vehicle Images SharePoint library.

Page | 15 2.a. 2.b. Figure 6 2.c. Application displaying images from a SharePoint list

2.d. 5. Click on one of the vehicle images in the list to select it.

Page | 16 6. The Windows Phone application displays a larger image from the SharePoint library.

2.e. 2.f. Figure 7 2.g. Viewing a single image from the SharePoint library

2.h.

Page | 17 7. Click the X in located above the image to close the image and return to the list of vehicle images.

Summary

3. In this hands-on lab, you saw how to use the lists.asmx Web service to retrieve images from a SharePoint Asset library and display them in a Windows Phone 7 application. You also learned how to use a converter to retrieve images from a URL and convert to a BitmapImage object which can be used to bind to an Image object in a Windows Phone 7 application.

Page | 18

Recommended publications