Thursday, July 21, 2011

Client Object Model – Part 2

In this post, we are going to see how we can use Silverlight client object model for SharePoint. If you have not gone through Part – 1 - managed client object model, I recommend you reading them first and then continue reading here.

We are going to create Silverlight project this time. So go ahead and add Silverlight application. You will see two projects loaded in solution. One is the web project which will generate the .xap file and the other is the client project that we need for our SharePoint.

A Silverlight application run on the browser that means it runs on the client side and whenever requires it interacts with serve with the help of WCF calls as far as database connectivity is concern. But in this model with SharePoint, we do not need to write anything extra with WCF. It has already been taken care of.

You need to add two DLL to the client project as shown in image below.

Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll

They are located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin

Create a simple UI in mainpage.xaml file

We need to understand that Silverlight application works on asynchronous model which means that once the process is started, we cannot block the UI. It works on the call back function for success and failure. So if you try to write down same line of code which we did in our previous part 1 post, it will not work at all.

We need to change the code this time due to Silverlight asynchronous model.

Write down following code to your xaml cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;

namespace SilverlightSPCOM
public partial class MainPage : UserControl
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();

public MainPage()

private void btnRetrive_Click(object sender, RoutedEventArgs e)

private void LoadData()

context = ClientContext.Current;

web = context.Web;


context.ExecuteQueryAsync(LoadSucces, LoadFailed);


private void DisplayData()
this.lblWebTitle.Text = web.Title;

this.lblWebDescription.Text = web.Description;

private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
UpdateUIMethod updateUI = DisplayData;

private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);


Because we will host this application inside SharePoint, we are not passing any URL for SharePoint site as we did in previous post. We will take reference of current SharePoint site.

When we click the button, we call function with two parameters which are method delegates. Respective method triggers based on the response we get from server. If we get success, then our first method will trigger or else the second. Observe that we call it asynchronously so that it does not block UI.

Build the solution.

Now to deploy this application in SharePoint we have two different options. One is we can deploy the generated .XAP file in ClientBin folder to some document library which has proper permission for users or we can deploy this XAP file to 14\TEMPLATE\LAYOUTS\ClientBin folder.

Now we are set with the deployment. All we have to do is open a page where you want to view this Silverlight application which accesses SharePoint data with client object model.

Edit the page, add Silverlight web part.

Once you add Silverlight web part, you will be prompt to give path of XAP file. If you have deployed in document library, give path of document library/file name.xap or else give path of 14\TEMPLATE\LAYOUTS\ClientBin\file name.xap.

Once done, save your changes on the page and see the simple magic happening on the page.

Read more in Part-3


Sudheer said...

Nice article,but a small correction i.e there will not be 'Text' property for Silverlight control,instead of that we can use 'Content' property of Silverlight label control as below

this.lblWebTitle.Content = web.Title;

this.lblWebDescription.Content = web.Description;

Sudheer said...

Without this code change it won't work

SharePoint Kings said...

Hi Sudheer,

This is tried and tested solution. That control is TextBlock not the lable. TextBlock does not have .content propoerty. it has .Text propoerty. Yes Label has .content propoerty.

That controls is TextBloxk, not label.

Thank you.

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to