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.
Property, returning value object is referred as scalar property.
Frequently Used Types:
Initializing Client Context:
ClientContext object requires the URL of site as a parameter to the constructor.
e.g. ClientContext context=new ClientContext(“http://www.somesite.com/”);
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:
|Site||Retrieves site-collection associated with current context|
|Web||Retrieves context specific site.|
|Current||Static 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.
|Load||Used to load the query for the action.|
|LoadQuery||Used to load the LinQ query & to perform queryable load. (see below)|
|ExecuteQueryAsync||Used 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:
private void Button_Click(object sender, RoutedEventArgs e)
context = ClientContext.Current;
site = context.Web;
private void OnSuccess(object sender, ClientRequestSucceededEventArgs e)