Search This Blog

Saturday, May 23, 2015

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

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



Create List Item:

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


Update List Item:

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:12345/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("");
            ListItem oListItem = oList.GetItemById(5);
            oListItem["Title"] = "Hello World Updated!!!";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}


Delete List Item:

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:12345/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("");
            ListItem oListItem = oList.GetItemById();         
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}


No comments:

Post a Comment