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:

http://[server]/_vti_bin/owssvr.dll?Cmd=Display&List=[Guid]&XMLDATA=TRUE

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?

Post a Comment