Friday, November 18, 2011

How does the SharePoint 2010 Client-Side Object Model work?


In my previous post we discussed about introduction to rest and the basics.


We have the client on the left and the server on the right. The server contains the SharePoint Databases and the Server-side Object model.



Between the server and client is a new WCF service called Client.svc. When you reference the client-side object model in your code,, the internal proxy class takes care of sending requests and review responses via the WCF Service.

The very important thing to notice here is that we don’t want to send requests to the server and get a response for almost every line of code we execute. So how it is designed to work is that in your code you will construct the objects and execute standard SharePoint OM functions. The objects will be empty structures without properties and without data, and the functions will not execute right away.



Then at logical points you bundle everything together and send it to the server for processing. The server will then unpack your bundled code and execute it in the correct order and return JSON to your client-side. Your objects will then be populated with data and properties and you can carry on executing more code. The important concept to understand is that you bundle empty objects together and send to the server when ready for processing.



The SharePoint Foundation 2010 managed client model consists of Two Assemblies that contains five namespaces.



There are 3 types of client object models.

1. Managed Client Object model


2-Silverlight Client Object model


3-ECMAScript Client Object model



1-Managed Client Object model:

This is used to extend Windows Form applications, services, WPF applications, console applications etc.

It consist of Microsoft.SharePoint.Client.dll (282 kb) and

Microsoft.SharePoint.Client.Runtime.dll (146 kb)



2-Silverlight Client Object model:

This is used to Silverlight Applications.

It consist of Microsoft.SharePoint.Client.dll (282 kb) and

Microsoft.SharePoint.Client.Runtime.dll (146 kb)



3-ECMAScript Client Object model:

JavaScript in our SharePoint user interface.

It consist of:

CUI.js (344 kb)

SP.js (381 kb)

SP.Core.js (13 kb)

SP.Ribbon.js (208 kb)

etc...



You will notice that there are a few differences between the objects in the Client-Side Object model and the Server-side Object model:


Lets build some simple code:

Create a new Visual Studio 2010 Console application

Add a references to:


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


Ensure your console app code look as follows:

using System;
using Microsoft.SharePoint.Client;

class demoSharePointClient {
static void Main() {
ClientContext clientContext = new ClientContext("http://yourappname /");
Web site = clientContext.Web;
clientContext.Load(site);
clientContext.ExecuteQuery();
Console.WriteLine("Title: {0}", site.Title); } }

Press control+F5 and see your code execute.

Lets analyze the code:

You inform the managed client OM about the operations that you want to take.

This includes accessing the values of properties of objects (for example, objects of the List class, ListItem class, and Web class), CAML queries that you want to run, and objects such as ListItem objects that you want to insert, update or delete.

Then you call the ExecuteQuery method. Only when you call the ExecuteQuery will the objects be bundled up and sent to the server for processing. No network traffic occurs before then.



Another Example:

Create a new Visual Studio 2010 Console application

Add a references to:

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

ensure your console app code look as follows:



using System;
using Microsoft.SharePoint.Client;


class Program {
static void Main() {


ClientContext clientContext = new ClientContext("http://yourappname /");
List list = clientContext.Web.Lists.GetByTitle("Announcements");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "";
ListItemCollection listItems = list.GetItems(camlQuery); clientContext.Load(list);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (ListItem listItem in listItems)
Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]); }}


Press control+F5 and see your code execute.

Again, the code will be bundled up and only be sent to the server for processing when the .ExecuteQuery() run.

This means that although you called the list.GetItems the CAML did not execute at that time.

When you call the .ExecuteQuery(), the server will receive your code, unpack it and execute it in the correct sequence and return the objects populated with data and properties.... all processed on the server side... and ready to be further used on the client-side !!!!

Cheers!!!

Suneet



















No comments: