In the previous post we set the stage for how can we start building Silverlight applications for SharePoint 2010
You have three options to actually program against the SharePoint from Silverlight: First there’s the SharePoint web services that have been there for a long time, Second there’s the new REST APIs that is suitable for working with strongly typed lists and the final option is the Client Object Model.
For Silverlight applications you need to reference the COM APIs there are two DLLs that you need to reference in your Silverlight project Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll, you can find these DLLs in the following path
“C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin”
So let’s start creating the Silverlight application, we will create two projects a SharePoint project and a Silverlight project and configure the SharePoint project to to deploy the Silverlight xap file (refer to the first post for more details).
in the Silverlight project, add reference to the two COM assemblies
we will build a simple Guest Book Silverlight Application, it will display a list of guests and allow the user to add new guests, here’s how the application looks like
The code is very straightforward, we have a view model that represents the Guests, the view model contains a collection of Guests that is bound to a ListBox in the xaml file, the add button is bound to a Command on the view model that adds a new Guest with the information entered by the user, the view model class is shown below
In the code behind file MainPage.xaml.cs of the main page, the view model is set as the DataContext of the entire page
The xaml is shown below
In the view model you can see that we are using a repository for the Guests implemented using the GuestStore class, this class simply stores the guests in an internal list
Till now this is a normal Silverlight application no SharePoint yet, what we will do is that, we will create a custom SharePoint list to store the Guests in it, and we will use our Silverlight application to populate this list, so let’s create the custom list in SharePoint.
From the Site Actions menu choose Create, in the Create Dialog select Custom List, name it GuestBook
Fill the necessary information and repeat this step for the three columns “First Name” , “Last Name” and “Email”
The next step is to change our Silverlight application to read/write to the GuestBook list. Most of the changes will be in the GuestsStore class, we will remove the internal list we used before.
The central object to start with while programming against the SharePoint COM is the ClientContext which represents the SharePoint Context in which the client (in our case the Silverlight application) is running, one important concept in SharePoint COM is that before you access a property or object you have to ask for it, let’s walkthrough the RetrieveGuests method
First we grab a reference to the current ClientContext, then we define an object that will reference our list (you can see that the object model is very similar to the SharePoint server API).
after that we ask the client context to load the list items
As you can see we explicitly asked the clientContext to Include the columns we are interested in.
The clientContext.Load doesn’t actually call to the server and retrieve the list item instead it just builds up the request that will be sent to the server, so basically we can batch multiple operations together to be executed as batch on the server, to actually send the request to the server we call the clientContext.ExecuteQuery method
We used the async version of the method, this method accepts a callback function to be called when ExceuteQuery succeeds
In this event handler we populate a list of Guests from the SharePoint list items and then fire a custom event GuestRetrieved, the view model subscribes to this event.
The AddGuest method is very simple, we define a new list item and set its field values, then submit this to the server, and fire an event if this succeeds
Here’s the application running inside SharePoint
This is just a simple basic Silverlight application, let’s add a simple enhancement that can make our Silverlight application shine within SharePoint, to do this we will use Expression Blend, right click on the MainPage.xaml file and select Open in Expression Blend
Right click on the ListBox and select Edit Additional Templates-> Edit Generated Item Container-> Edit a Copy
Click Ok in the Create Style Dialog
On the states tab set the transition duration for the LayoutStates group to 1 second
While the BeforeLoaded state is selected, select the grid element from the Objects and Timeline tool window, go to the properties window and change the Opacity property to 0% and change the Translate transform Y property to 100
The last thing is to click on the small icon to Turn on FluidLayout
Now build and run the application, you notice a very nice effect when adding elements to the list.
That’s the end of this post, hope this was useful.
You can download the source code from here
P.S: you will notice that the column names in the code don’t match the names we specified in SharePoint, please refer to this blog post for more details