Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to read and write to the maintenance announcements SharePoint list created in exercise 1.

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.AccBas.WS.sln file located at %TrainingKitPath%\Labs\IntegratingWebServices\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.AccBas.WSproject, 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 – 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.

  1. In the Solution Explorer, in the WP7.AccBas.WS project, right click Service References and select Add Service Reference.
  2. In the Addresstextbox enter the URL to the lists.asmx SharePoint web service for the site where you created the Maintenance Tasks list.
  3. Example: https://fbawp7/_vti_bin/lists.asmx
  4. Click Go.
  5. Once the service is resolved, enter SPListsService in the Namespace textbox.
  6. Click OK.

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

    Adding the enableHttpCookieContainer attribute

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

    Figure 5

    Expected exception when modifying a service with the enableHttpCookieContainer defined

    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 – Retrieving Maintenance Announcements from SharePoint

In this task, you will use the SharePoint lists.asmx Web service to return maintenance announcements from the SharePoint list.

  1. In the WP7.AccBas.WS project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 4.1.1 comment to define the LoadAnnouncements method:

    C#

    public void LoadAnnouncements() { XElement viewFields = new XElement("ViewFields", new XElement("FieldRef", new XAttribute("Name", "Title")), new XElement("FieldRef", new XAttribute("Name", "Body"))); AccBas.WS.SPListsService.ListsSoapClient lists = new AccBas.WS.SPListsService.ListsSoapClient(); lists.CookieContainer = App.CookieJar; lists.GetListItemsCompleted += new EventHandler<AccBas.WS.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: 4.1.2 comment to define the lists_GetListItemsCompleted method:

    C#

    void lists_GetListItemsCompleted(object sender, AccBas.WS.SPListsService.GetListItemsCompletedEventArgs e) { IEnumerable<XElement> rows = e.Result.Descendants(XName.Get("row", "#RowsetSchema")); IEnumerable<SPAnnouncement> announcements = from element in rows select new SPAnnouncement { Title = (string)element.Attribute("ows_Title"), Body = Utils.HtmlToText((string)element.Attribute("ows_Body")) }; Deployment.Current.Dispatcher.BeginInvoke(() => { if (Announcements == null) { Announcements = new ObservableCollection<SPAnnouncement>(); } Announcements.Clear(); announcements.ToList().ForEach(a => Announcements.Add(a)); }); }

    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 SPAnnouncement class that represents each maintenance announcement in the SharePoint list, and adds the SPAnnouncement instances to the Announcements observable collection. The Announcements 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.

  4. Save MainViewModel.cs.

Task 5 – Adding New Maintenance Announcement to SharePoint

In this task, you will use the SharePoint lists.asmx Web service to add a maintenance announcement to the SharePoint list.

  1. In the WP7.AccBas.WSproject project, in the ViewModels folder, open the AnnouncementViewModel.cs file.
  2. Add the following code under the //TODO: 4.1.3 comment to define the AddAnnouncement method:

    C#

    private void AddAnnouncement(SPAnnouncement announcemnt) { XElement updateQuery = new XElement("Batch", new XAttribute("OnError", "Continue"), new XAttribute("ListVersion", "1"), new XElement("Method", new XAttribute("ID", "1"), new XAttribute("Cmd", "New"), new XElement("Field", new XAttribute("Name", "Title"), announcemnt.Title), new XElement("Field", new XAttribute("Name", "Body"), announcemnt.Body))); AccBas.WS.SPListsService.ListsSoapClient lists = new AccBas.WS.SPListsService.ListsSoapClient(); //App.CookieJar used for SharePoint FBA Authentication. //See the Security With SharePoint And Windows Phone 7 Applications //module for more information. lists.CookieContainer = App.CookieJar; lists.UpdateListItemsCompleted += new EventHandler<SPListsService.UpdateListItemsCompletedEventArgs> (lists_UpdateListItemsCompleted); lists.UpdateListItemsAsync("Maintenance Announcements", updateQuery); }

    The above code uses the proxy class Visual Studio 2010 generated for the lists.asmx service to add a new announcement to the Maintenance Announcements SharePoint.

  3. Add the following code under the //TODO: 4.1.4 comment to define the lists_UpdateListItemsCompleted method:

    C#

    void lists_UpdateListItemsCompleted(object sender, SPListsService.UpdateListItemsCompletedEventArgs e) { Deployment.Current.Dispatcher.BeginInvoke(SaveCompleteAction); }

    The lists_UpdateListItemsCompleted method fires when the call to the lists.asmx SharePoint Web service completes. The method invokes the SaveCompleteAction delegate which was passed ito the view model via the Save method. In this example, the delegate will navigate the application back to the main page.

  4. Save theAnnouncementViewModel.cs.