Share via


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.
    1. Browse to the WP7.SharePoint.ListSchema.sln file located at %TrainingKitPath%\Labs\QueryingtheListSchemaforChoiceFields\Source\Before and select it.
    2. 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.
  4. The following code example demonstrates the value for a SharePoint server named fbawp7.

    C#

    public const string AUTHENTICATION_SERVICE_URL = "https://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.
  3. Example: https://fbawp7/_vti_bin/lists.asmx

    Figure 2

    Client Configuration Endpoint address

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

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 = "https://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"). 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.

    C#

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

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

    Contacts from SharePoint

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

    Contact details showing the choice field options