I have created one list named as Products in the SharePoint 2010 site with three columns "Title", "EMPID", "EMpName". I will be creating a Console Application using Visual Studio 2010 to do create, update and delete methods.
- Go to Visual Studio 2010.
- Go to File à New à Project.
- Select Console Application and name it as DMLSharePointList.
- Click Add.
- Add the references Microsoft.SharePoint.dll, Microsoft.SharePoint.Client.dll andMicrosoft.SharePoint.Client.Runtime.dll.
· 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();
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:
· 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 (3) item specified at the position 1 is deleted.
No comments:
Post a Comment