Client Object Model provides API to access SharePoint objects from a client machine. The core assembly involved is Microsoft.SharePoint.Client.
For example: You can access & modify SharePoint objects from a remote client machine.
What are the 3 types of Client Object Model?
Following are the 3 types:-
.Net
-
Silverlight
-
Mobile
How Client Object Model works?
SharePoint provides REST (Representational State Transfer) enabled web services that allow objects to be transferred in JSON format. Client Object Model assemblies in the background uses these web services to communicate with the server.Why we need Client Object Model?
The Server Object Model requires your code to be executed in one of the web servers on SharePoint is deployed. Client Object Model gives the advantage of execution from client machines.Almost all server object model class has an equivalent in client object model. Plus, client object model provides optimization mechanisms to reduce network traffic & increase speed.
Example Application
Now we can try creating a client object model application in .Net.Step 1: Create new project
Create a new project of type Windows Forms Application.
Step 2: Add Reference
Add reference to the following assemblies:
Step 3: Create Buttons
Now place the following buttons on the form & change their text.
Step 4: Create List Code
Now we can write the create list code. Place the following code in Create List button click event.
private void button1_Click(object sender, EventArgs e)
{
ClientContext context = new ClientContext("http://localhost");
Web web = context.Web;
ListCreationInformation info = new ListCreationInformation();
info.Title = "Client Object Model List";
info.TemplateType = 105; // Contact Template Id
web.Lists.Add(info);
context.ExecuteQuery();
}
You can execute the above code & back in SharePoint the new List is created.
You need to include using Microsoft.SharePoint.Client; The Form class also need to be kept as System.Windows.Forms.Form to resolve namespace collision.
Step 5: Insert Item Code
Now we can try inserting an item into the list. Place the following code in Insert Item button click event.
private void button2_Click(object sender, EventArgs e)
{
ClientContext context = new ClientContext("http://win2012");
List list = context.Web.Lists.GetByTitle("Client Object Model List");
ListItemCreationInformation info = new ListItemCreationInformation();
ListItem item = list.AddItem(info);
item["First Name"] = "SharePoint";
item["Last Name"] = "CTO";
item.Update();
context.ExecuteQuery();
}
Inside SharePoint you can see the new item is created.
Step 6: Update Item Code
Now we can try updating an item of the list. Place the following code in Update Item button click event.
private void button3_Click(object sender, EventArgs e)
{
ClientContext context = new ClientContext("http://localhost");
List list = context.Web.Lists.GetByTitle("Client Object Model List");
ListItem item = list.GetItemById(1);
item["First Name"] = "SharePoint (Changed)";
item.Update();
context.ExecuteQuery();
}
Inside SharePoint you can see the item is updated.
Step 7: Delete Item Code
Now we can try deleting an item from the list. Place the following code in Delete Item button click event.
private void button4_Click(object sender, EventArgs e)
{
ClientContext context = new ClientContext("http://localhost");
List list = context.Web.Lists.GetByTitle("Client Object Model List");
ListItem item = list.GetItemById(1);
item.DeleteObject();
context.ExecuteQuery();
}
Inside SharePoint you can see the item is deleted.
This completes our article on CRUD (Create, Read, Update & Delete) operations on list items.
Comments