Introduced with SharePoint 2010, the Client Object Mode helps developers design client applications that access SharePoint content without installing code on the server. The Client Object Model can run directly on client machines without having been installed on the SharePoint server.
How can I use the Silverlight Client Object Model to create a Silverlight Web Part to retrieve all list items, and display the returned list data in the Silverlight DataGrid?
SharePoint 2010 Silverlight Client Object Model
The table below lists some Client-side classes and server-side equivalents and, as you can see, it looks like the SharePoint Object Model. As a result, developers find it easy to manage and write code.
First, we must create a custom list named Profiles in our site collection, structured as shown in the following table:
Single line of text
Single line of text
Number (1, 1.0, 100)
Yes/No (check box)
Now, we begin creating the solution using the following steps:
1. Start Microsoft Visual Studio 2010.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, in the Installed Template pane, expand Visual C#, and then click Silverlight.
4. In the list of templates, click Silverlight Application.
5. In the Name box, type the name that you want to use for your project, such as SPClientOMDemo.
6. In the Location box, type the location where you want to place the solution, then click OK.
7. The New Silverlight Application dialog box appears as shown in the following illustration:
8. Uncheck the Host the Silverlight application in a new Web site check box.
9. Click the OK button to create the Silverlight Application.
10. Add the reference of SharePoint client object model right-clicking on the SPClientOMDemo project, and then clicking Add Reference… to open the Add Reference dialog box.
11. Select the Browse tab, navigate to the location of the Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll (They can be found at “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATELAYOUTSClientBin“). Select both DLLs, and then click OK as shown in the following illustration:
12. After you have added all references, the solution should look like this:
13. Using SharePoint Designer, open the Silverlight page (an XAML file) and drag and drop the DataGrid into the page:
Finally, the MainPage.xaml file will have contents that look like this:
14. Add a class named Profile to our project. This class will represent the SharePoint list that we are going to display in DataGrid. The Profile.cs file has the following contents:
15. Open the MainPage.xaml.cs file and add the reference.
Using Microsoft.SharePoint.Client, here is the MainPage.Xaml.cs file code:
Now that we have completed our solution, you can see that it’s very simple, right? In the above code, there’s just one point that we should take an additional interest in, namely:
By way of explanation, I’m going to quote the following text from an MSDN article on Using the Silverlight Object Model:
Deploy and Test:
We have two ways to deploy the Silverlight application package file (.xap) to the SharePoint environment:
- We can use a SharePoint document library and upload the Silverlight aplication package (.xap) file there. Refer to this file’s path location in the document library while adding the Silverlight Web Part; or
- We can use SharePoint’s default location [TemplatesLayoutsClientBin] and deploy the Silverlight application package (.xap) file there. Refer to this file path from the Silverlight Web Part.
In this post, we will use the second method to deploy and test.
1. Right-click on the project and navigate to the Build tab and set the output path to the following folder:
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATELAYOUTSClientBin
2. Build the project right-clicking the SPClientOMDemo project in Solution Explore and then selecting Build.
3. On our site collection, select the Page tab, then click Edit Page:
4. Click the Add a Web Part hyperlink:
5. On the Category pane, click Media and Content, select Silverlight Web Part on the Web Parts pane, and then click Add:
6. The Silverlight Web Part dialog box will be displayed, and we will input the URL of the Silverlight aplication package file (.xap). In our example, we will input the following url : /_layouts/ClientBin/SPClientOMDemo.xap
7. And finally, we will see the result:
To debug a Silverlight application, you must create a SharePoint project and use it to deploy a Silverlight Web Part. As a reference, see How to: Use a SharePoint Project to Deploy a Silverlight Application.