Introduction
In this article we start the development in the managed client object model. You will need to add references for the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.ClientRuntime.dll into the assemblies and add the using statement for the Microsoft.SharePoint.Client namespace and start writing the code.
Basic operations using Managed Client Object Model
In the managed client object, you can do all the SharePoint website related tasks such as read and write all the website related properties, create a new SharePoint web site, SharePoint List operations (create new SharePoint lists, retrieve all SharePoint lists in a SharePoint website, insert, update and delete in SharePoint lists), SharePoint Document Library operations (the same as SharePoint List operations).
Much like the server-side object model (that is the other framework for development in SharePoint) CSOM also needs a starting point in the form of a central object that will instantiate and provide access to the client object model. This central object is called the Client Context. The Client Context object orchestrates requests and initiates actions within a site collection. Once the Client Context Object is initialized, it provides information to the site collection or website through that we can access other SharePoint client objects remotely as depicted in the following code.
Example Code
Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.
Create List Item
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Client;
- namespace CreateListItem
- {
- class Program
- {
- static void Main(string[] args)
- {
- string siteUrl = "http://servername:2525/";
- ClientContext clientContext = new ClientContext(siteUrl);
- List oList = clientContext.Web.Lists.GetByTitle("TestList");
- ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
- ListItem oListItem = oList.AddItem(listCreationInformation);
- oListItem["Title"] = "Hello World";
- oListItem.Update();
- clientContext.ExecuteQuery();
- }
- }
- }
Update List Item
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Client;
-
- namespace UpdateListItem
- {
- class Program
- {
- static void Main(string[] args)
- {
- string siteUrl = "http://servername:2525/";
- ClientContext clientContext = new ClientContext(siteUrl);
- List oList = clientContext.Web.Lists.GetByTitle("TestList");
- ListItem oListItem = oList.GetItemById(5);
- oListItem["Title"] = "Hello World Updated!!!";
- oListItem.Update();
- clientContext.ExecuteQuery();
- }
- }
- }
Delete List Item
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Client;
-
- namespace UpdateListItem
- {
- class Program
- {
- static void Main(string[] args)
- {
- string siteUrl = "http://servername:2525/";
- ClientContext clientContext = new ClientContext(siteUrl);
- List oList = clientContext.Web.Lists.GetByTitle("TestList");
- ListItem oListItem = oList.GetItemById();
- oListItem.DeleteObject();
- clientContext.ExecuteQuery();
- }
- }
- }
Conclusion
Using the SharePoint Client object model, developers can work with the SharePoint web application remotely.