Saturday, April 10, 2010

SharePoint 2010 - Silverlight Client Object Model


Recently, I worked on a project based on SharePoint 2010 Beta. While providing solution to client, we came across Silverlight Client Object Model (OM). Microsoft SharePoint Foundation 2010 has it as part of new Client API.

As it is new to us, few questions come to our mind: How to use it? What are the basic steps to make it work? What objects, properties and methods available for use? We dig out the answer for the same & made use of it in our project successfully. I’m not the only one to have such questions, hence this post. Hope this will help others also.

Silverlight Client OM helps us develop client-side solutions. To use Silverlight Client OM, one needs to refer two DLL files (assemblies) in the application: Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll. The assemblies are located on SharePoint Server machine at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin” folder.

To start coding, one must add “using” statement for Microsoft.SharePoint.Client namespace. This core namespace provides types and members for working with SharePoint site-collection, sites, list data, and security. The Client API does not provide any admin objects or objects with scope higher than site collection.

All client objects inherit Microsoft.SharePoint.Client.ClientObject class & collections are inherited from Microsoft.SharePoint.Client.ClientObjectCollection class.

Client Object and Value Object:

All types, inheriting from ClientObject class, are referred as client objects whereas types, inheriting from ClientValueObject class, are value objects. Value objects have only properties & no methods. Do not conflict with Value Type concept of .NET Framework.

Scalar Property:

Property, returning value object is referred as scalar property.

Frequently Used Types:

All these types belong to “Microsoft.SharePoint.Client” namespace. “ClientContext” is the central object to gain access to Client OM.

Initializing Client Context:

ClientContext object requires the URL of site as a parameter to the constructor.

e.g. ClientContext context=new ClientContext(“”);

Silverlight web part when host the Silverlight application, they pass the URL of site as a initialization parameter (InitParams) to the application.

Properties of ClientContext:

SiteRetrieves site-collection associated with current context
WebRetrieves context specific site.
CurrentStatic property representing current context object.

By using these properties, one can access client objects up to the site-collection level.

Methods of ClientContext:
Below are the three important methods of ClientContext.

LoadUsed to load the query for the action.
LoadQueryUsed to load the LinQ query & to perform queryable load. (see below)
ExecuteQueryAsyncUsed to send the query to the server. This is an asynchronous operation.

Client Objects do not have associated data until data retrieval is performed. Data retrieval is done using the Load & Execute methods of ClientContext. Data retrieval can be executed in two ways: In-place load & queryable load.

Queryable load uses “LoadQuery” method of context object & returns enumerable generic collection. It means, retrieved data goes to result collection. It does not load the data in the object itself.

In-place load uses “Load” method of context & loads data to client objects. That means, retrieved data will be loaded to client objects.

LinQ has two forms of data projection syntax: query syntax & method syntax. “LoadQuery” should be used when using query syntax. For method syntax, either of the load methods works. LinQ in Client OM is LinQ-to-Object.

Once the query is loaded to the context, use ExecuteQueryAsync method to send the action query to server to get data. ExecuteQueryAsync has two parameters as delegate objects: ClientRequestSucceededEventHandler & ClientRequestFailedEventHandler.

ClientRequestSucceededEventHandler specifies the method to be executed after successful completion of ExecuteQueryAsync whereas ClientRequestFailedEventHandler invokes method on failure of query execution.

Steps for Data Retrieval

1. Get ClientContext object.
2. Specify the object to retrieve data using object model.
3. Load the object either using in-place load or queryable load.
4. Execute the query.
5. Write code to process object in a method which will execute after successful invocation query.

Data Retrieval Example:

using System;
using Microsoft.SharePoint.Client;

namespace Example.Silverlight.ClientOM
ClientContext context;
Web site;

public MainPage()

private void Button_Click(object sender, RoutedEventArgs e)
context = ClientContext.Current;
site = context.Web;


context.ExecuteQueryAsync(OnSuccess, null);
private void OnSuccess(object sender, ClientRequestSucceededEventArgs e)
MessageBox.Show(“Site Title:”+site.Title);

I know that this post requires few more examples. But due to time-constraint it is not possible. I'll update the post with few more samples and updates in few days. So visit again!!!
Post a Comment