SharePoint 2013: Create List Item Programmatically using CSOM

Introduction:

In this article we will learn how to create a List item in SharePoint 2013 using .NET Framework client object model (CSOM).

We can use the SharePoint client object model to create data in SharePoint 2013.
SharePoint makes the object model available in several forms.

  1. .NET Framework redistributable assemblies-CSOM & SSOM.
  2. JavaScript library
  3. REST/OData endpoints

The following article describes tasks that you can complete programmatically, and they include C# code examples that demonstrate CSOM operations.

When you create a Console Application in Visual Studio Select below assembly reference file,

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


Add below using statement to the code file.

usingMicrosoft.SharePoint.Client;

Run Visual Studio and select the Console application and name it Create a List Then type the below using Statement,

usingMicrosoft.SharePoint.Client;

Create a List item:

console

And select the assembly reference file in Visual studio,

reference

Source Code:

Then Copy the below csomcode and paste it in Visual Studio,

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. usingMicrosoft.SharePoint;  
  7. usingMicrosoft.SharePoint.Client;  
  8.   
  9.   
  10. namespaceCraete_ListItem   
  11. {  
  12.     classProgram   
  13.     {  
  14.         staticvoid Main(string[] args)   
  15.         {  
  16.             ClientContextctx = newClientContext("http://gowtham.sharepoint.com");  
  17.   
  18.             // Assume that the web has a list named "Announcements".   
  19.             List oList = ctx.Web.Lists.GetByTitle("Announcements");  
  20.   
  21.   
  22.             ListItemCreationInformationitemCreateInfo = newListItemCreationInformation();  
  23.             ListItemnewItem = oList.AddItem(itemCreateInfo);  
  24.             newItem["Title"] = "Test Item!";  
  25.             newItem["Body"] = "welcome to Gowtham Blog";  
  26.             newItem.Update();  
  27.   
  28.             context.ExecuteQuery();  
  29.         }  
  30.     }  
  31. }  
Output: Run F5 and check the sharepoint site.

output

Check the SharePoint Site. List item has been added successfully.