Tuesday, November 22, 2011

SharePoint 2010 : Client OM


SharePoint 2010: Client OM


Introduction



SharePoint 2007 allows using its Server OM to run against server running SharePoint and to communicate from the client side allows using the Web Services. SharePoint Client OM designed to run from the Client OS where SharePoint is not installed.
SharePoint 2010 introduces three new client APIs which can be used to interact with SharePoint sites. The three APIs are targeted for three different types of clients:

1. NET Managed Client OM (for the applications like console, window, web etc ) which are not running inside SharePoint Context.
2. Silverlight Client OM. (For the Rich UI applications using the Silverlight)

3. ECMA Script (Jscript, JavaScript). This API is only available for applications hosted inside SharePoint (for example, web part deployed in SharePoint site can use this JavaScript API for accessing SharePoint from browser using JavaScript). Cross Page Scripting is not possible with the ECMA Script.
The new ECMAScript (JavaScript, Jscript), .NET managed, and Silverlight client object models each provide a subset of the server object model that is defined in Microsoft.SharePoint.dll, including objects that correspond to major objects at the site-collection level or lower in the SharePoint Foundation hierarchy.

The client object models are provided through proxy .js and managed .dll files, respectively, which can be referenced in custom applications like other object models. The object models are implemented as a Windows Communication Foundation (WCF) service (.../_vti_bin/client.svc), but uses Web bindings to implement efficient request batching. All operations are inherently asynchronous, and commands are serialized into XML and sent to the server in a single HTTP request. For every command, a corresponding server object model call is made, and the server returns a response to the client in compacted JavaScript Object Notation (or JSON) format, which the proxy parses and associates with appropriate objects.

SharePoint 2007 had no Client Object model available.

 The main reason is that Microsoft has found lot of requests from SharePoint users to introduce more and more web services to get data out of SharePoint in the last couple of years. But introducing web services will not fix the issues, as Microsoft found, because then the request for more functionality in the web services will continue. Even if Microsoft provides a good numbers of web services with SharePoint, customization in web services will be required for different clients and this will make the out of the box web services unusable.

 The following table shows the equivalent objects in Client and SharePoint Object Model.

The Site class represents site collections, and the Web class represents sites.
Sample Code :
ClientContext clientContext = new ClientContext (siteUrl);
Site siteCollection = clientContext.Site;
Web site = clientContext.Web;

How Client OM is developed and work under the hood?

It’s interesting how SharePoint team has developed the same set of classes for three different sets of applications (Managed, Silverlight and ECMAScript). There is same class ListItem for three different set of applications

Client Type
Assembly/File
Managed Client
Microsoft.SharePoint.Client
Silverlight
Microsoft.SharePoint.Client.Silverlight
ECMAScript
SP.js

To ensure the same class object (say ListItem) behaves similarly in three different types of applications SharePoint team followed the steps described below:

a) SharePoint team first set attributes to the SharePoint classes and methods and properties that need to be exposed in Client OM.

b) Then a code generator is run against the SharePoint object model to generate client OM automatically.

This automated code generation has ensured maximum compatibility between these three sets of APIs.

How the Client-Side Object Model Works

An application that uses SharePoint content interacts with the API in several ways—call methods and get the return values, pass a Collaborative Application Markup Language (CAML) query and get the results, and set or get properties. After you use the API to perform a specific task the SharePoint Foundation 2010 managed client object model bundles up these uses of the API into XML and sends it to the server that runs SharePoint Foundation. The server receives this request, and makes appropriate calls into the object model on the server, collects the responses, forms them into JavaScript Object Notation (JSON), and sends that JSON back to the SharePoint Foundation 2010 managed client object model. The client object model parses the JSON and presents the results to the application as .NET Framework objects (or JavaScript objects for JavaScript). The following diagram shows these interactions.


SharePoint 2010 Namespaces:


Microsoft.SharePoint.Client is the core namespace used for the .NET managed and Silverlight object models, and SP is the core namespace for the JavaScript object model.

In addition to the core namespace, SharePoint 2010 provides the following namespaces.

.NET Managed and SilverlightJavaScript
Microsoft.SharePoint.Client.Application
N/A
N/A
N/A
N/A
N/A
N/A
N/A

The following table shows the equivalent objects that the new APIs provide for common SharePoint 2010 server objects.

Server.NET Managed and SilverlightJavaScript
Microsoft.SharePoint.SPContext
Microsoft.SharePoint.Client.ClientContext
SP.ClientContext
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.Client.Site
SP.Site
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.Client.Web
SP.Web
Microsoft.SharePoint.SPList
Microsoft.SharePoint.Client.List
SP.List
Microsoft.SharePoint.SPListItem
Microsoft.SharePoint.Client.ListItem
SP.ListItem
Microsoft.SharePoint.SPField (including major derived classes)
Microsoft.SharePoint.Client.Field
SP.Field
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager
Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager
SP.WebParts.LimitedWebPartManager

The new client APIs do not provide administration objects or objects that are higher than site collection: SPSite class in the server object model.









1 comment: