Monday, April 27, 2009

Silverlight: The easiest method to get SharePoint list data

In this post, I will share the simplest, most direct method I’ve found to access SharePoint list data from a Silverlight control hosted within a SharePoint web part. Contrary to most examples I’ve seen, this methodology will not require any custom web services. I’m am however going to assume that you have a working knowledge of how to build a simple SharePoint Web Part and a simple Silverlight control.

In learning new programming techniques, I’d rather read code examples than listen to some one bloviate, so for your reference this article uses four code listings which can be found in this online repository. These listings are:

Step 1: Getting the list data

In this step, we’ll use our old friend owssvr.dll to fetch an XML representation of our list data. For any given list in SharePoint, its XML data can get accessed using this URL:


In the above URL, Guid is the Id for your SharePoint list. To find out the Guid for your list, visit the list’s settings page and snag the Guid from the Url.

Step 2: Create the web part host

In Listing 1, we create a web part to host our Silverlight control. This web part will “lazy load” the Silverlight control’s compiled .xap file at runtime. I recommend uploading the .xap file to a SharePoint document library. This web part has 2 properties; the relative path to our .xap file and the Guid of our SharePoint list. Note that this technique still allows for complete step debugging.

Step 3: Silverlight Application file

In Listing 2, the Silverlight application file handles receiving parameters from our web part and passing the list Guid on to the Silverlight control itself.

Step 4: Silverlight Page code behind

The code in Listing 3 does most of the heavy lifting. This is where we fetch and parse the list data’s XML.

Step 5: Silverlight Page Xaml

Listing 4 is a simple Silverlight Xaml page with a DataGrid added.

And there you have it. I believe this technique to be the simplest, most direct method of fetching SharePoint list data for use in Silverlight. Questions or comments?


James said...

How is this easier (or better) than simply using a service reference to the standard SharePoint Lists web service (lists.asmx)? It gives you a much cleaner interface with SharePoint that gives you various methods to retrieve and modify list data.

Unknown said...

Hi James,

A lot of folks seem to have trouble getting things to work right when using the SharePoint web services approach from Silverlight.

The method in this article is just an alternative I find to be more expedient. I suppose it is a matter of opinion, but my example doesn't have any update requirements and it doesn't get much simpler than just having to parse an XML document.

Unknown said...

Hi Tony,

What's funny is that you're BOTH right. :) Either approach will work if you're more comfortable with the logic behind it. Thanks to both of you for the great material and suggestions. As one just getting started in Silverlight/Sharepoint development the more (fully explained) examples, the better.

Thanks again and keep up the great work!

Amy Grossman

Anonymous said...

Many thanks!
This exactly what i was looking for.

Corey Stone said...

Has anyone ever tried Tony's way with a particular view. I tried using the OWSSVR.DLL with the &List={}&View={} and even tried adding the &Source=http://... but none worked. I still get only the default view. Wondering if anyone had some thoughts.

milawo said...

Hi Tony. Thanks for your post.
But what if I need to know how many fields a certain view contains? How do I select something other than New Task but for instance the names of the different fields in a view?
Kind Regards Michael

Hemant Mahajan said...

Silver Light development is becoming common and is so easy to get employed in development, thanks for the post!! and making it so clear..

Deepak said...

IS there a way to get single item in the list in xml format

Deepak said...

By adding the a filter we can get item


can add more filters with FilterFeild2 FilterFeild3 and so on