Exercise 3: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to retrieve list data from the maintenance events SharePoint list created in exercise 1. This example uses anonymous access when retrieving items from the syndication feed.

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.Feed.Client.sln file located at %TrainingKit%\Labs\IntegratingSyndicationFeeds\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.Feed.Client, 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";

  5. Change the value for the FEED_URL constant to the URL specific to your development environment.
  6. The following code example demonstrates the value for a SharePoint server named fbawp7.

    C#

    public const string FEED_URL = "https://fbawp7/_layouts/WP7.AccBas.Feed.Svr/CustomListFeed.aspx";

Task 3 – Retrieving Maintenance Events from SharePoint

In this task, you will create an asynchronous call to the custom list feed application created in Exercise 2. This will retrieve all maintenance events in the SharePoint list formatted as an ATOM feed.

  1. In the WP7.AccBas.Feed.Client project, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 4.3.4 comment to define the LoadEvents method:

    C#

    private void LoadEvents() { string url = Constants.FEED_URL.TrimEnd('/') + "?ListTitle=Maintenance Events"; HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url); webReq.Method = "GET"; webReq.BeginGetResponse(new AsyncCallback(EventsResponseCallback), webReq); }

  3. The above code creates an HTTPWebRequest object and then makes a call to the custom list feed application passing in the title of the maintenance events list. The ListTitle parameter must be capitalized correctly or an error will occur. The HttpWebRequest executes asynchronously and calls back to the EventsResponseCallback function.
  4. Add the following code under the //TODO: 4.3.5 comment to define the EventsResponseCallback method:

    C#

    private void EventsResponseCallback(IAsyncResult asyncResult) { HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); try { string xmlEvents = ""; if (response.StatusCode == HttpStatusCode.OK) { using (var stream = response.GetResponseStream()) { Stream ReceiveStream = response.GetResponseStream(); xmlEvents = GetStreamAsText(ReceiveStream); } } ProcessAllEvents(xmlEvents); } catch (Exception Ex) { throw Ex; } }

  5. The EventsResponseCallback method fires when the call to the custom list feed application completes. The result is serialized XML which is captured in the xmlEvents variable. The string result is passed to the ProcessAllEvents method for processing.
  6. Add the following code under the //TODO: 4.3.6 comment to define the ProcessAllEvents method:

    C#

    private void ProcessAllEvents(string xmlEvents) { ObservableCollection<SPEvent> tmpEvents = new ObservableCollection<SPEvent>(); XmlReader reader = XmlReader.Create(new StringReader(xmlEvents)); SyndicationFeed feed = SyndicationFeed.Load(reader); foreach (var i in feed.Items) { DateTime startDateTime = DateTime.Parse(GetExtensionElementValue<string> (i, "EventDate")); DateTime endDateTime = DateTime.Parse(GetExtensionElementValue<string>(i, "EndDate")); SPEvent evt = new SPEvent() { Title = HttpUtility.HtmlDecode(i.Title.Text), Description = Utils.HtmlToText(HttpUtility.HtmlDecode(GetExtensionElementValue<string> (i, "Description"))), StartDate = startDateTime, EndDate = endDateTime, }; tmpEvents.Add(evt); } Deployment.Current.Dispatcher.BeginInvoke(() => { if (Events == null) { Events = new ObservableCollection<SPEvent>(); } Events = tmpEvents; }); }

    The ProcessAllEvents method parses the results from the call to the custom list feed application using the Syndication APIs. Each feed item is processed and results in a new SPEvent object which is added to the Events ObservableCollection property. The Events observable collection is bound to the MainPage user control in the Windows Phone 7 application. The MainPage user control displays the maintenance events retrieved from the SharePoint list via the custom list feed application.

  7. Save MainViewModel.cs.