Sunday 7 June 2015

SharePoint :Client Object Model



The concept of development using the Client Object model was introduced in SharePoint 2010 and has been carried to the new versions. With the introduction of client-side object model, developers have the ability to perform core SharePoint functionality by using C# (managed client object model), JavaScript and Silverlight.

Client Object Model is a new feature of SharePoint 2010. It provides features to program against a SharePoint site using .NET Managed Code or JavaScript.
Using Client Object Model we can access the data from the system where sharepoint is not installed on the machine through webservices, API and JavaScript.
The Client Object Model provides almost all the programming features of the Server Object Model plus advantages in deployment.

Language Flexibility:

We can use the following languages to work with the Client OM:
  • Microsoft .NET
  • Silverlight
  • ECMA Script (JavaScript /JScript)

Query Speed Optimizations:

In the Client OM, reduced network traffic is attained using Query Optimizations. Thus the user will feel reduced round trips and other advantages like paged results, etc.

Less Deployment Hassles:

Using Client OM, you do not need to install the components required by the Server Object Model. Thus Client OM provides much ease to the end user.

The Client OM works by sending an XML Request. The server will return a JSON response which is converted to the appropriate Object Model.

There are two assemblies to be referred for working with the Client Object Model.

    Microsoft.SharePoint.Client.dll
    Microsoft.SharePoint.Client.Runtime.dll

These assemblies can be found in the 14 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI.


Classes inside Client Object Model

In C#, comparing with classes of the Server Object Model, we can see that Client Object Model has similar classes with a suffix in the namespace and no SP prefix in the class name.
For example: SPSite in the Server Object Model is represented in the Client OM as Site with namespace Microsoft.SharePoint.Client.
Client Object Model Server Object Model
Microsoft.SharePoint.Client.ClientContext SPContext
Microsoft.SharePoint.Client.Site SPSite
Microsoft.SharePoint.Client.Web SPWeb
 Microsoft.SharePoint.Client.List SPList 



 

Basic operations with the SharePoint .NET client object model

SharePoint website tasks

  1. Retrieve the properties of a website
  2. Retrieve only selected properties of a website
  3. Write to website's properties
  4. Create a new SharePoint website

SharePoint list tasks

SharePoint list item tasks

SharePoint field tasks 

  1. Retrieve all of the fields in a list
  2. Retrieve a specific field from the list

SharePoint user tasks

  1. Add a user to a SharePoint group
  2. Retrieve all users in a SharePoint group
  3. Create a role
  4. Add a user to a role





 // Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// The SharePoint web at the URL.
Web web = context.Web; 

// Retrieve all lists from the server. 
context.Load(web.Lists, 
             lists => lists.Include(list => list.Title, // For each list, retrieve Title 
                                    list => list.Id)); 

// Execute query. 
context.ExecuteQuery(); 

// Enumerate the web.Lists. 
foreach (List list in web.Lists) 
{ 
    label1.Text = label1.Text + ", " + list.Title; 
} 







No comments: