Create, Update, and Delete a list Item using Client Object Model (CSOM) in SharePoint 2013/2016

This SharePoint tutorial, we will discuss how to work with SharePoint development using the managed client object model (csom). Here we will see how to create, update and delete a list item using the client object model (csom) in SharePoint 2013/2016.

Here, you will need to add references of Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.ClientRuntime.dll into the assemblies and add the using statement for Microsoft.SharePoint.Client namespace and start writing code.

You can add the dlls from the NuGet package or you can also install SharePoint 2013 client SDK on your machine.

Basic operations using Managed Client Object Model (CSOM) in SharePoint

Using managed client object, you can perform all SharePoint website-related tasks, ie- Read and write all website related properties, create 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 (same as SharePoint List operations).

Much like the Server-side object model, which is the other framework for development in SharePoint, CSOM also needs a starting point in the form of a central object which 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 which we can access other SharePoint client object remotely as depicted in the below code.

Example Code:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

Create List Item using Client Object Model (CSOM) in SharePoint

Below is the code to create a list item using the client object model (csom).

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 Client Object Model CSOM:

Below is the code to update a list item using the client object model (csom) in SharePoint 2013/2016.

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 Client Object Model CSOM:

The below code, we can use to delete a list item using client object model (CSOM) in SharePoint 2013/2016.

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();
}
}
}

You may like following SharePoint csom tutorials:

This SharePoint tutorial, we learned how to use the SharePoint Client object model, developers can work with the SharePoint Web Application remotely.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

>