SIGN UP MEMBER LOGIN:    
ARTICLE

How to Create/Update/Delete SharePoint 2010 list items using Client Object Model

Posted by Vijai Anand Articles | SharePoint November 22, 2010
In this article we are going to see how to create, update and delete SharePoint 2010 list items using Client Object Model.
Reader Level:

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

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

Create List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as CreateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    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:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
                ListItem oListItem = oList.AddItem(listCreationInformation);
                oListItem["Title"] = "Item1";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
        }
    }

  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.

    1.gif
Update List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as UpdateListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    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:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItem oListItem = oList.GetItemById(5);
                oListItem["Title"] = "My Updated Item.";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
        }
    }
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a item is updated.
Delete List Item:
  • Go to Visual Studio 2010.
  • Go to File => New => Project.
  • Select Console Application and name it as DeleteListItem.
  • Click Add.
  • Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

    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:39130/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("Test");
                ListItem oListItem = oList.GetItemById(5);         
                oListItem.DeleteObject();
                clientContext.ExecuteQuery();
            }
        }
    }
  • Hit F5.
  • Go to the SharePoint list "Test" and you could see a new item is added.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor