Search This Blog

Friday, October 10, 2014

SharePoint 2010 Client Object Model


Introduction to Client Object Model

Client Object Model is the newest feature of SharePoint 2010. It is the subset of server object model which the Microsoft has defined as Microsoft.sharepoint.dll. The primary function is to manipulate and consume share point data. Client Object Model Implement on Window communication Foundation (WCF) service (…/_vti_bin/client.svc)

Note:

1. To work on Client Object Model, you need Microsoft.sharepoint.Client.dll andMicrosoft.SharePoint.Client.Runtime.dll 
2. Location of dll will be as follows:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
3. You Need Microsoft.SharePoint.Client. Namespace on working with client object Model

Client Object Model is classified into different classes which are as follows –

ClientContext: ClientContext class is used to context information about such objects as site, site collection.
Site: Site class is used to get the site collection.
Web: Web class is used to get the web on current site collection.
List: List is used to get the list.
ListItem: ListItem class is used to get the ListItem.


How Client Object Model Work?

The Client OM send request as an XML format and server will return a JSON (java script Object Notation) which is converted into the appropriate Object Model.
This could be illustrated with the help of the following diagram. It is an easier way to access SharePoint 2010 data using Client Object Model –


How Client Object Model Work?
Client.svc:

Client object model is basically a WCF Web Service which is responsible for communication between client Object Model and Share Point Data. Client.svc is located along with all other SharePoint Web Services. The response from Client.svc Web Service is sent as JSON format.

Client.SVC service has one method (ProcessQuery ) which takes SteramObject
ProcessQuery method found in Microsoft.SharePoint.Client.ServerRuntime.dll and private class ClientRequestServiceImpl
Location of client.svc service: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Client.SVC


Why We Use Client Object Model?

1. Share Point installation is not required on development machine. If you want to develop a window application or any other application, you only require dll’s.

2. Client Object Model is user-friendly as it poses less deployment problems.

3. Flexibility in language: You can use any of the three different languages for Client Object Model

Microsoft .NET
Silverlight
ECMA Script (JavaScript /JScript)

4. Optimization in query speed: In Client OM, you do not need to install the SharePoint Server which is required by the Server Object Model. Thus, Client OM provides much ease to the end user.


Example of Client Object Model

//Method Add items into list
private void AddNewListItem()
{
var: clientContext = new ClientContext(“your site URL/”);
var :list = clientContext.Web.Lists.GetByTitle(“ListName”);
var: param = new ListItemCreationInformation();
var :newItem = list.AddItem(param);
newItem["Title"] = “This my new announcement”;
newItem["Body"] = “This is the body of announcement”;
newItem.Update();
clientContext.ExecuteQuery();
}

Posted by-
Deepak Chauhan 


Disclaimer: Developer’s Corner Section of ISHIR blog is contributed and maintained by independent developers. The content herein is not necessarily validated by ISHIR.


SharePoint Development options


Server Side Object Model
Client Object Model
REST/OData API
SharePoint Web Services

1.Server Side Object Model
-Full trust code using C# or VB.Net
We will use this option most of the times for on-prem SharePoint projects. Since the code will run in the SharePoint server itself. All your code bits will be deployed in SharePoint Server.
-You need add below dlls to develop SharePoint farm solutions
-Microsoft.SharePoint.dll
-Microsoft.SharePoint.Administration.dll
-You need to add below name space while developing SharePoint solutions(Farm solutions)
-Microsoft.SharePoint
-Microsoft.SharePoint.Administration

Some of the class names:
Under: -Microsoft.SharePoint.Administration
-SPFarm
-SPServer
-SPWebApplication
-SPDatabase
-SPContentDatabase
Under: -Microsoft.SharePoint
-SPContext
-SPSite (To access SharePoint sites. Ex:SPSite.RootWeb() will return root site of site collection)
-SPWeb  (To access all List and Libraries.Ex: SPWeb.Lists; will return all list objects) 
-SPList (To access list fields, items..)
-SPField
-SPListItem

2.Client Object Model

-.Net client side object model (CSOM). Using C# or VB.Net
-JavaScript Object Model (JSOM). Using javascript, jQuery
-SilverLight Object Model
CSOM -Will be used in on-prem environment or SharePoint App Development(Provider hosted app). We can't use SPO.
JSOM -Will be used in all environments. On-prem or SPO.

Why Client Object Model?
-Using Server Side Object Model, we can develop solutions and we have to deploy that in SharePoint server.
But take a scenario, one Custom Application (Which is not SharePoint Application. Ex: some Windows client app) has to communicate with SharePoint.
For this we can develop client application using SharePoint Web Services, which will access in Windows client app, so no need to deploy this code SharePoint server.
But the limitation is, We don't have enough Web Services where we can access all SharePoint features using web services.
So what we have to do is, every time, we have to develop custom web services on SharePoint and use that service in Windows client app. Because of this everyone is investing their time and money to write custom web services.
So Microsoft is providing new way to access SharePoint objects with out running that code in to SharePoint server, that is called Client Object model.
View below image..


Client Object Model: No need to deploy in SharePoint Server, we can access SharePoint objects from client system.
How we can write Client Object Model? 
A)Using JSOM (Javascript), CSOM(.NetCLR using C# or VB.Net), SilverLight

If your client is using only javascript then you can use JSOM to access SharePoint. You need to add:SP.js and SP.Runtime.js
If your client is using only Asp.Net with C# or Vb.Net then you can use CSOM (C# or VB.Net) to access SharePoint.You need to add:
Microsoft.SharePoint.Client.dll,Microsoft.SharePoint.Client.Runtime.dll
If your client is using only SilverLight then you can use SilverLight to access SharePoint.You need to add:
Microsoft.SharePoint.Client.dll,Microsoft.SharePoint.Client.Runtime.dll

View below image


How Client Object Model Work?
View below image



3.REST/OData API
-URL based api
-REST: Representational state transfer
-URL based protocol
-Technology and platform independent

4.SharePoint Web Services
SharePoint has provided some web services to access SharePoint data, we can use that services in client application.


No comments:

Post a Comment