Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint...

13
Hands-On Lab Querying the List Schema for Choice Fields Lab version: 1.0.0 Last updated: 9/4/2022 Page | 1

Transcript of Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint...

Page 1: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

Hands-On LabQuerying the List Schema for Choice Fields

Lab version: 1.0.0

Last updated: 5/24/2023

Page | 1

Page 2: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

CONTENTS

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

EXERCISE 1: CREATING A SHAREPOINT CONTACT LIST...................................................................4Task 1 – Creating a Contact List...........................................................................................................4

Task 2 – Adding a Custom Field to Track Contact Type........................................................................4

Task 3 – Adding Sample Contacts to the List.......................................................................................5

EXERCISE 2: QUERY THE LISTS WEB SERVICE FOR CHOICE FIELD OPTIONS...............................6Task 1 – Beginning the Exercise...........................................................................................................6

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

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

Task 5 – Implementing Code to Get the Contact List Choice Field Schema.........................................7

Task 6 – Implementing Code to Display the ContactType Field and Available Choices........................9

Task 8 – Testing the Application........................................................................................................10

SUMMARY................................................................................................................................................ 11

Page | 2

Page 3: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

Overview

SharePoint choice list columns provide the flexibility to add and change the values available to end users. For example a Contact list may contain different types of contacts: Customers, Vendors, Suppliers, Etc. When writing applications that use such a list, it is a best practice to request the list schema and load the list of choices dynamically so that changes to the list do not require recompiling the application.

In this lab, you will create an application that reads a Contact list with a custom choice field. You will use the SharePoint Lists Web service to get the available values for the choice field so that your application provides the same field validation the user would receive while working with SharePoint in a web browser.

Objectives

In this hands-on lab, you will learn how to create a Windows Phone 7 application that connects to a Contact list to display contact details including a Choice field. This lab details how to query the Lists Web service to determine the choices available for a field.

Learn how to use the SharePoint Lists Web service to query the list schema.

Learn how to use Linq to determine the Schema for a specific field.

Learn how to populate a ListBox control with the correct values from the list schema.

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

Page | 3

Page 4: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

◦ 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

Exercise 1: Creating a SharePoint Contact List

In this exercise, you will create a Contact list and add a custom Choice field called Customer Type for use in this lab. You will add 3 contacts to the list and assign different values for the Customer Type field. The Windows Phone 7 application will read this list and populate a list box with the appropriate values for the list.

Task 1 – Creating a Contact List

In this task, you will create a standard Contact list. This sample data is used in this lab.

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 View All Site Content.

4. On the All Site Content page, click Create.

5. In the Create dialog, choose the Contacts list.

6. Name the list Contacts.

7. Click Create.

Task 2 – Adding a Custom Field to Track Contact Type

In this task, you will use the Contacts list and add a new column to track Contact Type.

1. In the ribbon, on the List Tools | List tab, click Create Column.

2. In the Create Column dialog, enter the following information:Page | 4

Page 5: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

a. Column Name: ContactType

b. Column Type: Choice

c. Choices (Type each choice on a separate line):

i. Customer

ii. Vendor

iii. Partner

iv. Other

3. Click OK.

Task 3 – Adding Sample Contacts to the List

In this task, you will add sample data to the Contact list for display in the Windows Phone application.

1. In the ribbon, on the List Tools | Items tab, choose New Item.

2. Enter the following information in the New Item dialog:

a. Last Name: Jump

b. First Name: Dan

c. Full Name: Dan Jump

d. E-mail Address: [email protected]

e. Business Phone: 888.555.1111

f. ContactType: Customer

3. Click Save.

4. In the ribbon, on the List Tools | Items tab, choose New Item.

5. Enter the following information in the New Item dialog:

a. Last Name: Poe

b. First Name: Toni

c. Full Name: Toni Poe

d. E-mail Address: [email protected]

e. Business Phone: 888.555.2222

f. ContactType: Vendor

6. Click Save.

7. In the ribbon, on the List Tools | Items tab, choose New Item.

Page | 5

Page 6: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

8. Enter the following information in the New Item dialog:

a. Last Name: Martinez

b. First Name: Frank

c. Full Name: Frank Martinez

d. E-mail Address: [email protected]

e. Business Phone: 888.555.3333

f. ContactType: Partner

9. Click Save.

10. Confirm that you have three contacts with all of the above fields completed.

Figure 1Contact List with Data

Exercise 2: Query the Lists Web Service for Choice Field Options

In this exercise, you will create a Windows Phone 7 application to query the Contacts SharePoint list. You will implement the code necessary to determine what values are available for the Customer Type choice field.

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.

Page | 6

Page 7: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

a. Browse to the WP7.SharePoint.ListSchema.sln file located at %TrainingKitPath%\Labs\QueryingtheListSchemaforChoiceFields\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.People 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 – Configuring the Reference to the SharePoint Lists.asmx Web Service

In this task, you will configure the reference to the SharePoint lists.asmx Web service. The service reference to this Web service has already been added to the project.

1. In the Solution Explorer, double click the ServiceReferences.ClientConfig file to open it.

2. In the Endpoint element, change the address attribute to the URL for the lists.asmx SharePoint Web service in the site where you created the Sample Tasks list.

Example: http://fbawp7/_vti_bin/lists.asmx

Figure 2Client Configuration Endpoint address

Task 5 – Implementing Code to Get the Contact List Choice Field Schema

Page | 7

Page 8: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

In this task, you will use the SharePoint Lists Web service to determine the valid values for the Contact list ContactType field.

1. In the WP7.SharePoint.ListSchema project, expand the ViewModels folder, and double click the MainViewModel.cs file.

2. Add the following code under the //TODO: 10.3.1 comment to define the ContactTypes collection:

C#

/// <summary>/// A collection for ContactTypes objects./// </summary>public ObservableCollection<string> ContactTypes { get; private set; }

3. Add the following code under the //TODO: 10.3.2 comment to initialize the ContactTypes collection.

C#

ContactTypes = new ObservableCollection<string>();

4. Add the following code under the //TODO: 10.3.3 comment to call the Lists Web service and parse the result to determine the choices for the ContactType field.

C#

private void LoadChoices(){ SPListsService.ListsSoapClient lists = new SPListsService.ListsSoapClient(); lists.CookieContainer = App.CookieJar; lists.GetListCompleted += new EventHandler<SPListsService.GetListCompletedEventArgs>(lists_GetListCompleted); lists.GetListAsync("Contacts");}

void lists_GetListCompleted(object sender, SPListsService.GetListCompletedEventArgs e){ //Declare the namespace XNamespace nsDocument = "http://schemas.microsoft.com/sharepoint/soap/"; //Use Linq to get the values of the //elements under CHOICES for the ContactType field var choiceElements = e.Result.Descendants(nsDocument + "Field").

Page | 8

Page 9: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

Where(f => f.Attribute("Name").Value.Equals("ContactType")) .SelectMany(f => f.Elements(nsDocument + "CHOICES").Elements(nsDocument + "CHOICE")) .Select(f => f.Value );

//Clear the ContactTypes collection ContactTypes.Clear(); //For each item in our list of results add a ContactType choiceElements.ToList().ForEach(choiceElement => ContactTypes.Add(choiceElement));}

5. The LoadChoices method above uses the GetList Web method to request the list schema for the Contacts list. When the asynchronous result returns, the lists_GetListCompleted method uses Linq to place the CHOICE elements into a variable and then the elements are loaded into the ContactTypes collection.

Note: See the Security With SharePoint And Windows Phone 7 Applications module for more information about security and the App.CookieJar property.

6. Add the following code under the comment //TODO: 10.3.4 to call the LoadChoices method.

C#

LoadChoices();

Task 6 – Implementing Code to Display the ContactType Field and Available Choices

In this task, you will edit the user interface to display the ContactType field in a ListBox and bind the ItemsSource to the ContactTypes collection.

1. In the WP7.SharePoint.ListSchema project, double click the ContactView.xaml file.

2. Add the following code under the <!-- TODO: 10.3.5 Add the ContactType List Box -->.

XAML

<ListBox Height="182" Margin="0" x:Name="lbContactTypes" />

3. In the WP7.SharePoint.ListSchema project, expand the ViewModels folder, and double click the ContactView.xaml.cs file.

4. Add the following code under the //TODO: 10.3.6 comment to bind the ContactTypes collection to the List box named lbContactTypes.

Page | 9

Page 10: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

C#

lbContactTypes.ItemsSource = App.ViewModel.ContactTypes;lbContactTypes.SelectedItem = viewModel.DataModel.ContactType;

The code above binds the list box ItemsSource to the ContactTypes collection that you created from the GetList Web method. The selected item is set to the current ViewModel’s ContactType value.

Task 8 – Testing the Application

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

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

2. Press F5.

The Windows Phone 7 application starts in the emulator. The application will initially display the three contacts that you added to the contact list.

Figure 3Contacts from SharePoint

Page | 10

Page 11: Querying the List Schema for Choice Fieldsaz12722.vo.msecnd.net/.../Lab.docx · Web viewSharePoint choice list columns provide the flexibility to add and change the values available

Clicking any contact will navigate the application to the Contact View page. Notice for each contact that the full list of Contact Types is visible with the current Contact’s Contact Type highlighted.

Figure 4Contact details showing the choice field options

Summary

In this hands-on lab, you saw how to create a Windows Phone 7 application that can query the available values for a SharePoint choice field. This level of validation ensures that users will only enter valid values for items in SharePoint lists and when list choices are changed in SharePoint you will not have to recompile your application to fetch the new values.

Page | 11