How to Build a RSS Reader for Windows Phone 7

Exclusive offer: get 50% off this eBook here
Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7 — Save 50%

Create enterprise-ready websites and applications that access Microsoft SharePoint on Windows Phone 7 with this book and eBook

$29.99    $15.00
by Todd Spatafore | June 2011 | Enterprise Articles Microsoft

This article will introduce programming Windows Phone 7 applications. We will use RSS feeds that are provided from lists in SharePoint to display information to the Windows Phone 7 user. Throughout the article, we will build a simple RSS Reader application for Windows Phone 7 and add complexity to it as we go along.

In this article by Todd Spatafore, author of Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7, we will cover the following:

  • Security in SharePoint 2010
  • Using WebClient to get data from the web
  • RSS feeds available from SharePoint
  • Parsing XML in Windows Phone 7

 

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

Create enterprise-ready websites and applications that access Microsoft SharePoint on Windows Phone 7

        Read more about this book      

(For more resources on Microsoft Sharepoint, see here.)

Security in SharePoint 2010

We begin this article with a discussion on security for a very simple reason: security in SharePoint is tricky. In addition to that one very simple reason, authenticating users against SharePoint from within a Windows Phone 7 client application is even trickier.

In this article, the example RSS reader that we develop will only use anonymous access to the RSS feeds in SharePoint. Setting up anonymous access is very simple and we'll walk through the steps here.

When writing this article, I came across a lot of errors on my testing server, but not my development server. After a couple of days of unsuccessful web searches and reinstalling different components, I discovered the root of my problem was due to the fact that the SharePoint 2010 Prerequisites install a non-final version of ADO. NET Data Services 1.5. Make sure the final version is installed from Microsoft. More information is available at the following URL: http://blogs.msdn.com/b/astoriateam/archive/2010/01/27/data-services-update-for-net-3-5-sp1-available-for-download.aspx

There are two places where we need to make changes to our SharePoint site to enable anonymous access:

  • Central Administration
  • Site Permissions

Central Administration

Classic mode authentication is more or less just classic Windows authentication using NTLM or Kerberos.

Although Internet Explorer Mobile in Windows Phone 7 can do the NTLM authentication, as we've been doing up to now to view SharePoint sites in the browser, the version of Silverlight that is included in Windows Phone 7 cannot currently use this authentication mechanism.

Carry out the following steps to configure Central Administration for anonymous access:

  1. From the Start menu, select All Programs.
  2. Find the folder named Microsoft SharePoint 2010 Products in the list of programs and click on it.
  3. Click on SharePoint 2010 Central Administration.
  4. You need to select the Yes option on the User Account Control dialog that may appear.

At this point, the home page for SharePoint 2010 Central Administration should appear as displayed in the following screenshot:

(Move the mouse over the image to enlarge.)

Next, click on Manage web applications. The page that appears lists out all of the web applications in the SharePoint site. There should be two items listed here, but it is possible there are more. Select the main website by clicking on its name. This main website is usually titled SharePoint – 80. Once selected, the ribbon bar across the top should light up, as all the icons become active. Carry out the following steps to enable anonymous access:

  1. Click on the Authentication Providers icon.
  2. In the Authentication Providers dialog that appears, select the Default link.
  3. The Edit Authentication dialog box will appear. In the third section, down under a heading of Anonymous Access there is a check box listed as Enable anonymous access. Check that box.
  4. Scroll all the way to the bottom of the dialog box and select the Save button.
  5. SharePoint 2010 will process the request. Then, return to the Authentication Providers dialog box and close it.

There is one more section that may need tweaking and if this is a production environment, it should be considered. That section is Anonymous Policy. Click on the icon for it in the ribbon and a dialog box will appear. From here, we can customize the anonymous policy for the site.

  • None – No policy basically leaves the door open for read and write access from anonymous users. This is a good policy to use when anyone who can access the site should also be able to modify the content of the site. This is a good policy for Wiki's.
  • Deny Write – Has no write access allows the anonymous users to read the site, but they cannot write, update, or delete content. This is a good policy to use for sites that we want to specify particular authenticated accounts write access, but allow everyone the ability to read the content. Some Wiki's use this policy and a lot of blogs use this policy.
  • Deny All – Has no access selecting this removes all permissions to the anonymous user. Secure content should always use this policy.

Site Permissions

Once we have updated the web application to allow anonymous access, we have to give anonymous users access to the site collection. To do this, we close Central Administration and open our SharePoint site. Once on the site, select Site Permissions from the Site Actions menu.

This will open the Permissions home page. In the ribbon at the top, there is an icon named Anonymous Access. Click on that button and the Anonymous Access dialog will appear, as shown in the following screenshot:

This dialog has three radio buttons to fine tune the access that anonymous users have. The key point to remember here is that although we are really opening up the site for everyone to see, we can break the inherit permissions model on a child page at any time if we need to remove anonymous access.

Now that we have opened up our SharePoint site to anonymous users, we can begin to write applications for Windows Phone 7. In a production environment, we may not have the privilege of opening a SharePoint site this wide, but remember that we are only doing it for demonstration purposes here. We have to walk before we can run. Now, let's get started on the RSS reader.

 

Using WebClient to get data from the web

As was stated in the introduction to this article, we are going to build a really simple RSS reader for Windows Phone 7. We are going to keep everything really simple. What that means is that we are going to focus on the pieces of code that actually do something.

This is what we are going to do:

  • Create our base project
  • Add a text block to display the WebClient results
  • Create a WebClient
  • Use the WebClient to request the contents of our SharePoint home page
  • Display the raw HTML that is returned in the text block on the page

First, a quick word about WebClient. WebClient isn't the most robust method of making requests over a network, but it's really simple and works for simple cases, such as the one we are working with.

Creating the base project

We can start by creating our base project. This RSS reader will use the Visual Studio Silverlight for Windows Phone Windows Phone Application template. Carry out the following steps to start the project:

  1. Open Visual Studio 2010.
  2. Select File from the main menu and then select New Project….
  3. In the New Project dialog box that appears, select Silverlight for Windows Phone.
  4. Then select Windows Phone Application from the list of templates for Windows Phone.
  5. Give the project a name, for example SimpleRSSReader.
  6. Give the Solution a name, for example Chapter06.
  7. Change the Location as desired and click on the OK button.

At this point, Visual Studio will go off and create the solution and project. When it has finished, MainPage.xaml will appear on the screen in split screen mode, as shown in the following screenshot:

Displaying WebClient results by adding a text block

The first thing we are going to do here is add a text block to the content panel. Add the following code to the Grid that has a name of ContentPanel.

<TextBlock x:Name="webClientResults" />

This creates a text block named webClientResults and puts it in ContentPanel. We could spruce this up a bit by providing a font size, or padding, but we are going to keep this really simple and only show the code needed to get things done.

Save the progress.

Creating a WebClient

Open up the code behind by either clicking on it in the top tab bar, double-clicking the file name in Solution Explorer, or press F7 while in the XAML code.

In the code behind, create a private member variable, outside the constructor, named client of type WebClient and a private member variable, also outside the constructor, named siteUrl of type string. The siteUrl should have a value that is the URL to your SharePoint home page.

private WebClient client = new WebClient();
private string siteUrl = "http://ssps2010/";

These are the variables that we'll be using in just a minute. The first is the WebClient that makes the requests on the network. The second is the Url for our SharePoint home page. This is the address that the WebClient will use to request a web page.

Requesting the contents of our SharePoint home page

Now that we have a WebClient, let us do something with it. Add the following code to the Main Page constructor:

client.DownloadStringCompleted += new DownloadStringCompletedEventHand
ler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(siteUrl));

The first line adds a new event handler to the DownloadStringCompleted event. This event handler is called client_DownloadStringCompleted and we will write it shortly. The second line is what starts an asynchronous request to our SharePoint home page to get the HTML content.

Displaying the raw HTML that is returned

Until now, we've created a place in the content panel to display our results. We've created a couple of variables. We've added a new event handler for when the WebClient finishes and we've made the web request for our SharePoint home page. Next, we are going to receive the result from the WebClient.

Earlier, when we added a new event handler to the WebClient, we told WebClient that when it finishes downloading the string, it should call our method named client_DownloadStringCompleted. The following is the code for that method:

void client_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e) {
if(e.Error == null) {
webClientResults.Text = e.Result;
}
}

First, we check to see if there is an error. To make this as simple as possible, we are not handling the situation where there is an error. We only care if there are no errors. Always check that errors are null. If there is an exception in the WebClient request, the DownloadStringCompletedEventArgs Error property will contain an object of type Exception. These exceptions can range from network connections being down, which is common for cell phones, to invalid URLs.

We then take the result of the web request and put it in the text block we created earlier. Save the progress and press F5 to see the application run on the Windows Phone 7 Emulator.

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

In the preceding screenshot, ApplicationTitle and PageTitle have also been updated. Both of these text blocks are found in the XAML in TitlePanel.

We have successfully used WebClient to read data from the web and display it on the screen. It is still in a raw format though, and it isn't much of an RSS reader, especially since this page isn't even RSS. We will get there, but first let's find some RSS in SharePoint.

 

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7 Create enterprise-ready websites and applications that access Microsoft SharePoint on Windows Phone 7 with this book and eBook
Published: May 2011
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

 

        Read more about this book      

(For more resources on Microsoft Sharepoint, see here.)

RSS feeds available from SharePoint

One of the really cool things in SharePoint is that almost every list in it has an RSS feed associated with it. Unless specifically set to not emit an RSS feed, all lists have a feed associated with them.

Carry out the following steps to ensure RSS is enabled on a site collection:

  1. From the site's home page, select Site Settings from the Site Actions menu.
  2. On the Site Settings page, under the Site Administration section, select the RSS link.
  3. This opens up the page that allows us to enable or disable RSS feeds. Ensure that the Site Collection RSS and Enable RSS section check boxes are checked.
  4. Add in any of the Advanced Settings information that may be required. We won't be using it in our demos, but in production these might be needed.
  5. Finally, click on the OK button and SharePoint will save the settings.

The basic question of how to customize the feed from each list comes up a bit and that procedure is actually quite simple.

  1. Navigate to the list that we want to customize.
  2. At the top, click on List in the List Tools section.
  3. In the list ribbon, click on the List Settings icon. If the icon is disabled, you may be viewing the site anonymously and will need to login from the link at the top left.
  4. Under Communications is a link for RSS settings. Click on that link.

The Modify RSS Settings page has a lot of information that can be customized, from the ability to turn off RSS altogether, down to what columns from the list should be included in the RSS description. The following example is a screenshot of this page:

After customizing the content, click on the OK button to save changes.

Now that we have the feed customized, where's the link? Good question. Back on the default view for the list, click on List in the List Tools menu to display the list ribbon. The list ribbon includes a link for the RSS Feed in the middle. Click on it and the feed will open in the browser. Copy the URL because we will use it in the next step of our RSS reader.

 

Parsing XML in Windows Phone 7

Let's go back to the application that we are building in Visual Studio. First, change siteUrl in the MainPage.xaml.cs file to the RSS Feed URL that we got from our list.

private string siteUrl = "http://ssps2010/chapter6/_layouts/listfeed.
aspx?List=%7BD45848A1%2D12E8%2D4722%2DAB5B%2D511BF802F8D8%7D&Source=h
ttp%3A%2F%2Fssps2010%2Fchapter6%2FLists%2FEngineering%2520Notes%2FAll
Items%2Easpx";

Now, rerun the application in the emulator and the raw XML from the RSS feed will appear in the text block. Now that we have the XML feed in a string, we should display it in a more meaningful way. To do that, let's do the following:

  1. Replace the text block display with a list box
  2. Create a simple view model for an RSS Item
  3. Parse the XML
  4. Create a list of RSS Items
  5. Bind the list of RSS Items to the list box

Sounds simple, right? Before we begin though, a quick word about a term we used in step 2. Although we are going to create a view model that will hold each RSS item, we are not following a strict MVVM (Model View ViewModel) pattern here. We are simply utilizing a view model to assist in binding the data to the view. If this application grew up to be a fully featured RSS reader, we would probably want to refactor it to an MVVM pattern, but that is an optimization that we simply don't need right now. Also, it flies in the face of attaining the simplest code that will get the job done here.

Replacing the text block display with a list box

The first step here is to replace the text block that we were using to dump the string output with a list box, where we can display each individual RSS item. To do that, open MainPage.xaml and delete the TextBlock with a name of webClientResults. In its place, enter the following XAML:

<ListBox x:Name="feedListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Date}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Working from the inside out, we are going to display each RSS item's title and date. We do this with text blocks and bind the text property to the data for the particular list box item. As the titles can get long, we've included text wrapping on the title.

These text blocks are placed inside a stack panel. This means that the title will be stacked on top of the date. We have also set the width of the stack panel. Without this width, the title text block won't wrap properly.

This stack panel is placed inside the data template for the list box item template. This list box item template is a property of the list box that we've replaced our text block with.

The name of this list box is feedListBox. In the code behind, we will bind the RSS item list to the items source for this list box. Save the progress.

Creating a simple view model for an RSS Item

Before we can bind the RSS items list to the list box, we need to have a list of RSS items. Before we can have a list of RSS items, we need to define what an RSS item is. This is where the view model comes in. Carry out the following steps to create a class that will house our view model:

  1. Right click on the SimpleRSSReader project in Solution Explorer.
  2. In the context menu that appears, select Add and then select the Class… option.
  3. This will open the Add New Item dialog with the Class template already selected.
  4. In the Name field, name the new class as RSSItem.cs.
  5. Click on the Add button and Visual Studio will create the class and open it in the main IDE window.

In the class, add the following code:

public string Title { get; set; }
public string Date { get; set; }
public string PostUrl { get; set; }

This creates three public properties that we will use for our RSS feeds. Although RSS items have more fields associated with them, these are the only three that we will need for this exercise.

Something of note in these properties is the Date field. The publish date of a post is DateTime, but we are using it as a string here for the simple fact that we don't really need a DateTime. When we parse the XML in the next step, it will save us from having to convert from string to DateTime and then back to string when we data bind it to the text block. Save the progress.

Parsing the XML

We've replaced the text block with a list box and created a simple view model for our RSS items. Next, we will parse the XML string that was returned from the WebClient.

  1. In Solution Explorer, right click on References for the SimpleRSSReader project.
  2. In the Add Reference dialog box, find System.Xml.Linq and add it to the project.
  3. Open the Main Page code behind.
  4. Add this using statement to the top of the page: using System.Xml.Linq;
  5. Locate the client_DownloadStringCompleted event handler.
  6. Delete this line of code: webClientResults.Text = e.Result;
  7. Add the following code where the preceding line of code was:
    var result = e.Result;
    var rssElement = XElement.Parse(result);
  8. Save the file.

The first thing we did here was to add a reference to Windows Phone 7's LINQ to XML namespace. This contains classes, such as XElement that we use to parse the XML string, as well as the LINQ engine that we will use in the next step to create the list of RSS items.

Language-Integrated Query, LINQ, was introduced into the .NET Framework in Version 3.5. It provides a general purpose syntax that allows for powerful data processing in .NET. More information on LINQ is available at the following URL:
http://msdn.microsoft.com/en-us/library/bb308959.aspx

Next, let's create a list of RSS Items.

Creating a list of RSS Items

Now we have a place to put the RSS items, we have a model of what an RSS item looks like, and we have an XElement with our RSS feed stuffed in it. The next thing to do is take the XElement and perform a LINQ statement to get a list of RSS Items.

In the previous step, we parsed the XML into a variable named rssElement. Immediately after that, add the following statement:

var items = from item in rssElement.Descendants("item")
select new RSSItem
{
Title = item.Element("title").Value,
Date = item.Element("pubDate").Value,
PostUrl = item.Element("link").Value
};

Wow, that is one statement. What we are doing here is searching through the XElement for all item elements. Once we have an item element, we call it an item and use it to create a new RSSItem. Lines 4, 5, and 6 parse the item element further to get the string values for specific elements in the item element.

Again at this point, you'll notice in line 5 that we could have parsed the publish date as a DateTime, but since we will only ever use it as a string, it's easier to just grab the value as a string.

Once the RSSItem has been created, it is added to the list of RSS items in the variable items. We will use this variable to bind to our list box.

Binding the list of RSS Items to the list box

We've come to the point where we take the list of RSS items and actually bind that data to the list box we created earlier. This is done in a single line of code, as follows:

feedListBox.ItemsSource = items;

Save the file, build, and then run the application in the Windows Phone 7 Emulator. You will see something similar to the following screenshot:

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

It's a bit hard to read, so let's go back to the XAML and make some changes to the text blocks.

First, change the Title text block as follows:

<TextBlock Text="{Binding Title}" TextWrapping="Wrap"
Style="{StaticResource PhoneTextTitle2Style}" />

This adds a style from the static resources included with the phone that is more fitting a title.

Next, change the Date text block as follows:

<TextBlock Text="{Binding Date}"
Style="{StaticResource PhoneTextAccentStyle}" />

This adds the accent color from the phone to the date to make it stand out a bit, as shown in the following screenshot:

Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7

Windows Phone 7 has customization available to the end user, which allows them to select between 10 different accent colors and an overall theme of dark or light. The advantage of using the built-in Static Resources for Windows Phone 7's styles is that when the end user changes the theme color from dark to light or the accent color to one of the 10 options on the phone, your application will pick it up and look like it's more of a part of the phone.

This works well if that is the goal of your application. If we want to maintain a certain corporate branding though, we should create a custom resource file which contains all of our corporate color schemes and use that resource data.

Summary

In this article, we took the first steps towards building Windows Phone 7 applications. We first briefly looked at SharePoint security and how to enable anonymous access. Next, we started our simple RSS reader by creating a WebClient to read data from the network and display it on the screen.

Next, we investigated the RSS options available in SharePoint and looked at how to customize those feeds. From those RSS feeds, we used our WebClient to load that data and then parse the XML into a list of RSS items. This was by no means the only way to parse RSS, but it was the simplest way.


Further resources on this subject:


Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7 Create enterprise-ready websites and applications that access Microsoft SharePoint on Windows Phone 7 with this book and eBook
Published: May 2011
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

About the Author :


Todd Spatafore

Todd Spatafore is a professional web developer and software architect who enjoys living life on the bleeding edge of technology. He is an expert on HTML, CSS, JavaScript, ASP.NET (WebForms and MVC), C#, and Silverlight. Todd is currently the Director of Technology at Draftfcb and maintains his own blog at http://www.spatacoli.com, in which he muses about current programming topics such as Silverlight, JavaScript, HTML, CSS, and Hyper-V. Currently Todd is working on a few independent Windows Phone 7 apps, and speaks for MSDN conferences on web application architecture, RIA development in Silverlight, and Windows Azure.

Todd Spatafore is the author of “Microsoft SharePoint 2010 Enterprise Applications on Windows Phone 7".

Books From Packt


Microsoft SharePoint 2010 End User Guide: Business Performance Enhancement
Microsoft SharePoint 2010 End User Guide: Business Performance Enhancement

Microsoft SharePoint 2010 Administration Cookbook
Microsoft SharePoint 2010 Administration Cookbook

Microsoft SQL Azure Enterprise Application Development
Microsoft SQL Azure Enterprise Application Development

Microsoft Data Protection Manager 2010
Microsoft Data Protection Manager 2010

Microsoft Dynamics Sure Step 2010
Microsoft Dynamics Sure Step 2010

Microsoft SharePoint 2010 Power User Cookbook: SharePoint Applied: RAW
Microsoft SharePoint 2010 Power User Cookbook: SharePoint Applied: RAW

Microsoft Dynamics AX 2009 Administration
Microsoft Dynamics AX 2009 Administration

Microsoft Silverlight 4 Data and Services Cookbook
Microsoft Silverlight 4 Data and Services Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software