A very basic SilverLight app using SharePoint ClientOM (Client Object Model)

First off, you are going to create a XAP file, just like any other SilverLight project. In doing so, you’ll be using the SP ClientOM (Client Object Model). As such, you’ll need a SharePoint context from which to run your SL app. In other words, you have to deploy this to your SharePoint site. If you want to run it from a space outside a SharePoint context, then good luck with XSS (Cross-Site Scripting).

OK. Let’s get started. First off, we crank out Visual Studio and create a new SilverLight application. We’ll also let it create a Web site for us, but we’re not going to use it in this sample.

Next, we’ll add references to our SharePoint ClientOM. We need to find the DLLs in the SharePoint Root under Bin (that is “14/bin”).

On our new cancas, we’ll add a stack panel so we can easily line up a button and a textbox. We’ll let the textbox wordwrap, too.

On our code-behind file, we’ll add a using directive. 

 “Wait. Only 1?” you ask. “Didn’t we reference two DLLs.”

Yes, only one even though we did reference two files. Let’s take a look at their names spaces using the Object Browser. You’ll notice the Runtime uses the same namespace.

The next three images show the entire class.

In the second image, you’ll see we’re handling the button event and calling a method. That method sets the context, creates a query, and then sends the query off to the server. Because the SP ClientOM for Silverlight ONLY works asynchronously, the app needs to know which method to call for a success and which to call for a failure. Either way, execution will return on a behind-the-scenes thread.

Because we need to update the GUI, yet the execution returns on a seperate thread, we need to jump back onto the primary thread. To do so, we have to call Dispatcher.BeginInvoke() and give it a delegate. And we have to do this for success and fail. Imagine if we have a bunch of queries to run on the server. We wind up with several methods. To keep things simple, I use a common method for failures. This is somewhat limiting in functionality. If we need a custom error message for each point of failure, then we’ll need a unique fail method for each query, too.

Here is the bin directory. Keep this handy. You’ll upload the .xap from here into SharePoint.

In SharePoint, I’ve created a new page. However, any page will do.

In this page, we Insert a SilverLight WebPart.

The SilverLight webpart needs a url. Enter the url from the xap file loaded into a document library above.

And here we have our webpart on the page.

Finally, the fruit of our labor.

So we now have a XAP file that will query SharePoint for all lists in the site. Next, we’ll attempt to display the lists in a grid, allowing the user to select any list. Upon doing so, the app will display all items from that selected list.


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: