Thursday, April 22, 2010 8:48 AM Jan Tielens

Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Consuming SharePoint 2010 data in Windows Phone 7 applications using the CTP version of the developer tools is quite a challenge. The issue is that the SharePoint 2010 data is not anonymously available; users need to authenticate to be able to access the data. When I first tried to access SharePoint 2010 data from my first Hello-World-type Windows Phone 7 application I thought “Hey, this should be easy!” because Windows Phone 7 development based on Silverlight and SharePoint 2010 has a Client Object Model for Silverlight. Unfortunately you can’t use the Client Object Model of SharePoint 2010 on the Windows Phone platform; there’s a reference to an assembly that’s not available (System.Windows.Browser).

My second thought was “OK, no problem!” because SharePoint 2010 also exposes a REST/OData API to access SharePoint data. Using the REST API in SharePoint 2010 is as easy as making a web request for a URL (in which you specify the data you’d like to retrieve), e.g. http://yoursiteurl/_vti_bin/listdata.svc/Announcements. This is very easy to accomplish in a Silverlight application that’s running in the context of a page in a SharePoint site, because the credentials of the currently logged on user are automatically picked up and passed to the WCF service. But a Windows Phone application is of course running outside of the SharePoint site’s page, so the application should build credentials that have to be passed to SharePoint’s WCF service. This turns out to be a small challenge in Silverlight 3, the WebClient doesn’t support authentication; there is a Credentials property but when you set it and make the request you get a NotImplementedException exception.

Probably this issued will be solved in the very near future, since Silverlight 4 does support authentication, and there’s already a WCF Data Services download that uses this new platform feature of Silverlight 4. So when Windows Phone platform switches to Silverlight 4, you can just use the WebClient to get the data. Even more, if the OData Client Library for Windows Phone 7 gets updated after that, things should get even easier! By the way: the things I’m writing in this paragraph are just assumptions that I make which make a lot of sense IMHO, I don’t have any info all of this will happen, but I really hope so.

So are SharePoint developers out of the Windows Phone development game until they get this fixed? Well luckily not, when the HttpWebRequest class is being used instead, you can pass credentials! Using the HttpWebRequest class is slightly more complex than using the WebClient class, but the end result is that you have access to your precious SharePoint 2010 data. The following code snippet is getting all the announcements of an Annoucements list in a SharePoint site:

HttpWebRequest webReq =
    (HttpWebRequest)HttpWebRequest.Create("
http://yoursite/_vti_bin/listdata.svc/Announcements");
webReq.Credentials = new NetworkCredential("username", "password");

webReq.BeginGetResponse(
    (result) => {
        HttpWebRequest asyncReq = (HttpWebRequest)result.AsyncState;

        XDocument xdoc = XDocument.Load(
            ((HttpWebResponse)asyncReq.EndGetResponse(result)).GetResponseStream());

        XNamespace ns = "http://www.w3.org/2005/Atom";
        var items = from item in xdoc.Root.Elements(ns + "entry")
                    select new { Title = item.Element(ns + "title").Value };

        this.Dispatcher.BeginInvoke(() =>
        {
            foreach (var item in items)
                MessageBox.Show(item.Title);
        });
    }, webReq);

When you try this in a Windows Phone 7 application, make sure you add a reference to the System.Xml.Linq assembly, because the code uses Linq to XML to parse the resulting Atom feed, so the Title of every announcement is being displayed in a MessageBox. Check out my previous post if you’d like to see a more polished sample Windows Phone 7 application that displays SharePoint 2010 data.
When you plan to use this technique, it’s of course a good idea to encapsulate the code doing the request, so it becomes really easy to get the data that you need. In the following code snippet you can find the GetAtomFeed method that gets the contents of any Atom feed, even if you need to authenticate to get access to the feed.

delegate void GetAtomFeedCallback(Stream responseStream);

public MainPage()
{
    InitializeComponent();

    SupportedOrientations = SupportedPageOrientation.Portrait |
        SupportedPageOrientation.Landscape;

    string url = "http://yoursite/_vti_bin/listdata.svc/Announcements";
    string username = "username";
    string password = "password";
    string domain = "";

    GetAtomFeed(url, username, password, domain, (s) =>
    {
        XNamespace ns = "
http://www.w3.org/2005/Atom";
        XDocument xdoc = XDocument.Load(s);

        var items = from item in xdoc.Root.Elements(ns + "entry")
                    select new { Title = item.Element(ns + "title").Value };

        this.Dispatcher.BeginInvoke(() =>
        {
            foreach (var item in items)
            {
                MessageBox.Show(item.Title);
            }
        });
    });
}

private static void GetAtomFeed(string url, string username,
    string password, string domain, GetAtomFeedCallback cb)
{
    HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
    webReq.Credentials = new NetworkCredential(username, password, domain);

    webReq.BeginGetResponse(
        (result) =>
        {
            HttpWebRequest asyncReq = (HttpWebRequest)result.AsyncState;
            HttpWebResponse resp = (HttpWebResponse)asyncReq.EndGetResponse(result);
            cb(resp.GetResponseStream());
        }, webReq);
}

Filed under: , ,

Comments

# Twitter Trackbacks for Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 - Jan Tielens' Bloggings [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 - Jan Tielens' Bloggings         [asp.net]        on Topsy.com

# Planning for SharePoint 2010 Migration; Microsoft Tests Facebook Office Docs; McAfee Update Whacks XP PCs

Thursday, April 22, 2010 9:07 AM by SharePoint Daily

Top News Stories 5 Tips on Effectively Planning for SharePoint 2010 Migration (CMS Wire) Mixed feelings

# What is the most healthy kinds of foods to have for Breakfast?

Pingback from  What is the most healthy kinds of foods to have for Breakfast?

# Discover How To Make Money With I-Phone Applications! | Treating Arthritis

Pingback from  Discover How To Make Money With I-Phone Applications! | Treating Arthritis

# Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 … Staff

Pingback from  Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 … Staff

# Facebook May Not Be Skynet, But It Is Getting Smarter and That's … | facebook

Pingback from  Facebook May Not Be Skynet, But It Is Getting Smarter and That's … | facebook

# hotel travel jakarta sentra – Nevaeh Burns | travel hotel

Pingback from  hotel travel jakarta sentra – Nevaeh Burns | travel hotel

# Sullivan County Talk: NYC OTB bailout could cost Yonkers Raceway … | Sullivan County NY Real Estate

Pingback from  Sullivan County Talk: NYC OTB bailout could cost Yonkers Raceway … | Sullivan County NY Real Estate

# Should I adopt a cat thru the Pet Smart adopt a pet program … | feed cat

Pingback from  Should I adopt a cat thru the Pet Smart adopt a pet program … | feed cat

# New Hair Color ?

Thursday, April 22, 2010 8:09 PM by New Hair Color ?

Pingback from  New Hair Color ?

# sea world san diego_sea animals_sea world san antonio_sea temperature_sea world orlando » What is the normal ammount of sleep that an 18 yr old girl should have during the week?

Pingback from  sea world san diego_sea animals_sea world san antonio_sea temperature_sea world orlando » What is the normal ammount of sleep that an 18 yr old girl should have during the week?

# Crafts and Curios from NOfkantsCurios: Folksy Friday Fossil Inspired!

Pingback from  Crafts and Curios from NOfkantsCurios: Folksy Friday Fossil Inspired!

# How do I string one of those string driven roller blinds with pulleys at the top? Diagrams welcome. Thanks ? | Blinds Shades

Pingback from  How do I string one of those string driven roller blinds with pulleys at the top?  Diagrams welcome.  Thanks  ? | Blinds Shades

# travel hotel accomodations holland michigan – Hailey Pagano | travel hotel

Pingback from  travel hotel accomodations holland michigan – Hailey Pagano | travel hotel

# Marr Haven Wool Farm Blog: Spring Green hand dyed yarn

Friday, April 23, 2010 9:30 PM by Marr Haven Wool Farm Blog: Spring Green hand dyed yarn

Pingback from  Marr Haven Wool Farm Blog: Spring Green hand dyed yarn

# Who's Not Evil Now? Facebook's Social Connections vs Google's … | facebook

Pingback from  Who's Not Evil Now? Facebook's Social Connections vs Google's … | facebook

# How to Restore Your Privacy on Facebook – Facebook – Gawker | facebook

Pingback from  How to Restore Your Privacy on Facebook – Facebook – Gawker | facebook

# How do you get black sharpie marker out of a white cotton blouse? :Apparel Outlet USA

Pingback from  How do you get black sharpie marker out of a white cotton blouse? :Apparel Outlet USA

# How to increase my wireless internet signal and improve my internet speed? | Tanit Internet World

Pingback from  How to increase my wireless internet signal and improve my internet speed? | Tanit Internet World

# re: Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Saturday, May 01, 2010 11:06 PM by Xwnizhck

comment 2:& www.d3kicks.com/.../read.php kdmtlnq

# re: Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Sunday, May 02, 2010 5:47 PM by Kbuatild

comment 2:(!) www.d3kicks.com/.../read.php bhmwprk

# re: Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Monday, May 03, 2010 8:16 PM by Oyewcvkz

comments!!! best diet pills :D lqjpiod

# re: Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Monday, May 03, 2010 8:16 PM by Whjpufqg

Here that it is possible to tell!!! www.d3kicks.com/.../read.php zyengvo

# re: Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7

Monday, May 03, 2010 8:18 PM by Wtibuapv

your comment2:& <a href=www.d3kicks.com/.../read.php acomplia</a>  vkdntib

# Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 | OOP - Object Oriented Programing

Pingback from  Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7 | OOP - Object Oriented Programing