In this article we are going to see how to create, update and delete SharePoint 2010 list items using Client Object Model through C#. 

I have created one list named as Products in the SharePoint 2010 site with three columns "Title", "ProductID", "ProductName". I will be creating a Console Application using Visual Studio 2010 to do create, update and delete methods.

 

Insert, Update, Delete in SharePoint 2010 list using Client Object Model

  • Go to Visual Studio 2010.
  • Go to File à New à Project.
  • Select Console Application and name it as DMLSharePointList.
  • Click Add.

Insert, Update, Delete in SharePoint 2010 list using Client Object Model

  • Add the references Microsoft.SharePoint.dll, Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

Insert, Update, Delete in SharePoint 2010 list using Client Object Model

·         Write the following code in the Program.cs file:

using System;
using System.Text;
using Microsoft.SharePoint.Client;
 
namespace DMLSharePointList
{
    class Program
    {
        private void Insert()
        {
            string siteUrl = "http://rohit:34143/"//sharepoint site address
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList =clientContext.Web.Lists.GetByTitle("Products");
            ListItemCreationInformation listCreationInformation = new                                                                                  ListItemCreationInformation();
            ListItem oListItem =oList.AddItem(listCreationInformation);
            oListItem["Title"] = "6";
            oListItem["ProductID"] = "P006";
            oListItem["ProductName"] = "MotherBoard";
            oListItem.Update();
            clientContext.ExecuteQuery();
            Console.WriteLine("Item Added");
       
        }
        private void Update()
        {
            string siteUrl = "http://rohit:34143/"//sharepoint site address
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("Products");
            ListItem oListItem = oList.GetItemById(1);
            oListItem["Title"] = "1";
            oListItem["ProductID"] = "P001";
            oListItem["ProductName"] = "Harddisk";
            oListItem.Update();
            clientContext.ExecuteQuery();
            Console.WriteLine("Item Updated");
 
        }
        private void Delete()
        {
            string siteUrl = "http://rohit:34143/"//sharepoint site address
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("Products");
            ListItem oListItem = oList.GetItemById(1);
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
            Console.WriteLine("Item Deleted");
 
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            byte ch = 0;
            do
            {
                Console.WriteLine("\t\t\t----Select option----\t\t\t");
                Console.WriteLine("\t\t\t----Press (1) For Insert ----\t\t\t");
                Console.WriteLine("\t\t\t----Press (2) For Update ----\t\t\t");
                Console.WriteLine("\t\t\t----Press (3) For Delete ----\t\t\t");
                Console.WriteLine("\t\t\t----Press (0) For Exit ----\t\t\t");
                ch = Convert.ToByte(Console.ReadLine());
                switch (ch)
                {
                    case  1:
                        obj.Insert();
                        break;
                    case 2:
                        obj.Update();
                        break;
                    case 3:
                        obj.Delete();
                        break;
                    default:
                        Console.WriteLine("Invalid Option");
                        break;
                }
            } while (ch != 0);
        }
    }
}
 

After creating an application press F5 to debug your application:

Insert, Update, Delete in SharePoint 2010 list using Client Object Model

·         When you Press (1) specified item is added in the SharePoint list. Simultaneously, you can also check in the SharePoint list (Products) that one row is added in the list.

·         When you Press (2) item specified at the position 1 is updated.

·         When you Press (3) item specified at the position 1 is deleted.

Thanks for reading this article. I think this will help you a lot.


  Modified On Nov-30-2017 05:56:17 AM
  1. How do I modify the code to add value to a person field?

    Thanks,

    Alberto da Silva

    avs270@gmail.com

Leave Comment