Posted by DevExpert on 7th February 2009
I’ve done a little work in the recent months with Silverlight, specifically with leveraging Silverlight inside of SharePoint. My first project involved a pulling in data from SharePoint via a WCF service, and while it worked, it took a long time to configure correctly and to add the necessary components to SharePoint in order for it to work. Sahil Malik describes a method beginning here, and as you can see it’s definitely doable but not the simplest approach.
While creating a WCF will offer you the most power and flexibility when it comes to the data it can expose, as you can manipulate SharePoint data (or any data for that matter) any way you like. In this post, I’m going to show you how to retrieve SharePoint list data in Silverlight the “old-fashioned” way – by using the out-of-the-box SharePoint web services, and executing an HttpWebRequest to get our data.
Because there are a ton of different concepts I could elaborate on related to Silverlight and SharePoint, such as creating a web part host, configuring SharePoint for Silverlight, etc., for the sake of this post I’m going to keep it focused on the data retrieval only. In future posts, I’ll dive into my approach for hosting a Silverlight app in SharePoint. Also, take a look at this post from Karine Bosch – she has a very similar approach and hers is a lot prettier than mine!
Next, I created a new Silverlight application in Visual Studio, and defined a Widget class, which will serve as the representation of a Widget list item:
I also added a few class-level variables in my Page.xaml.cs file, which I’ll use in my data methods:
For the data retrieval, I’m going to be leveraging the out-of-the-box Lists.asmx web service to retrieve the list items, and build an HttpWebRequest object. The BeginGetRequestStream method executes the request, and will asynchronously call the RequestCallback method when it finishes:
Here’s where the real work needs to be done. In our callback method, we need to build a SOAP envelope that will be passed to the web service in order to get a valid response. If you’re uncertain where to find that envelope or are unsure if how it should be formatted, simply browse to the web service itself and click the method that you will be executing:
Once the SOAP envelope format is determined, simply add in the listName, viewName (if necessary), query, viewFields, and queryOptions, and you have your envelope. The next step is to get the response by asynchronously executing the BeginGetResponse method and passing in the ResponseCallback callback method:
The ResponseCallback method is responsible for determining that the response is valid, and to get the data out of the response. Once the data is retrieved from the response stream, the only step left is to process that data, and for that we call ProcessResponse. We must use the BeginInvoke() method of the page’s Dispatcher object, because all of the data retrieval is done asynchronously on a different thread. Since you can’t modify controls on a different thread than the one it was created on, we have to invoke it this way and let Silverlight handle the thread marshalling:
And finally, the ProcessResponse method that parses the response data and creates are Widgets collection, which is then set as the page’s DataContext:
Now that we have our data, creating a snazzy Silverlight web part is only limited by your creativity and XAML-slinging skills. Here’s a quick and dirty snippet to verify we’re actually getting data:
…and voila! Silverlight in SharePoint consuming list data:
That’s all it takes to get data from SharePoint without using a custom web service or having to go through the hassle of building and deploying a WCF service. In a future post, I’ll describe how to build a generic and reusable web part to host Silverlight applications. Stay tuned!