Create, Update, Delete a List Using Client Object Model (CSOM) SharePoint 2013

Posted: July 20, 2014 in SharePoint2013

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Client;
  7. namespace CreateListItem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string siteUrl = http://servername:2525/”;
  14.             ClientContext clientContext = new ClientContext(siteUrl);
  15.             List oList = clientContext.Web.Lists.GetByTitle(“TestList”);
  16.             ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
  17.             ListItem oListItem = oList.AddItem(listCreationInformation);
  18.             oListItem[“Title”] = “Hello World”;
  19.             oListItem.Update();
  20.             clientContext.ExecuteQuery();
  21.         }
  22.     }
  23. }

Update List Item

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Client;
  7. namespace UpdateListItem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string siteUrl = http://servername:2525/”;
  14.             ClientContext clientContext = new ClientContext(siteUrl);
  15.             List oList = clientContext.Web.Lists.GetByTitle(“TestList”);
  16.             ListItem oListItem = oList.GetItemById(5);
  17.             oListItem[“Title”] = “Hello World Updated!!!”;
  18.             oListItem.Update();
  19.             clientContext.ExecuteQuery();
  20.         }
  21.     }
  22. }

Delete List Item

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Client;
  7. namespace UpdateListItem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string siteUrl = http://servername:2525/”;
  14.             ClientContext clientContext = new ClientContext(siteUrl);
  15.             List oList = clientContext.Web.Lists.GetByTitle(“TestList”);
  16.             ListItem oListItem = oList.GetItemById();
  17.             oListItem.DeleteObject();
  18.             clientContext.ExecuteQuery();
  19.         }
  20.     }
  21. }

Conclusion

Using the SharePoint Client object model, developers can work with the SharePoint web application remotely.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s