+91-90427 10472
         
Dot net training in Chennai -Maria Academy

CRUD operation in Client side object model C#.net

16 Mar 2017

Document by Ganesanva@hotmail.com – + 919042710472

This Blog helps you to do CSOM Crud operation in SharePoint 2010 using Managed C# code.
Create a List “Student Details” in Share point as shown below,


Create a new Console Application as shown below,

Click Ok.
Add References in the Solution as shown in Solution Explorer,

Replace the Class Program with the below code,

class Program
{
static void Main(string[] args)
{
string siteUrl = “http://htc:7004/“;
ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle(“Student Details”);
startconsole:
Console.WriteLine(“Enter your Option:”);
Console.WriteLine(“1. Create New item in Student Details List”);
Console.WriteLine(“2. Update item in Student Details List”);
Console.WriteLine(“3. Get list of items in Student Details List”);
Console.WriteLine(“Enter 1 or 2 or 3”);
int optionvalue = Convert.ToInt32(Console.ReadLine());
if (optionvalue == 1)
{
Console.WriteLine(“Enter EStudent Name”);
string EmpName = Console.ReadLine();
Console.WriteLine(“Enter Student Age”);
string Age = Console.ReadLine();
Console.WriteLine(“Enter Student Code”);
string Code = Console.ReadLine();
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem[“Title”] = EmpName;
oListItem[“Student_x0020_Age”] = Age;
oListItem[“Student_x0020_Code”] = Code;
oListItem.Update();
clientContext.ExecuteQuery();
Console.WriteLine(“Inserted successfully”);
}
else if (optionvalue == 2)
{
CamlQuery oquery = new CamlQuery();
ListItemCollection oListItemCollection = oList.GetItems(oquery);
clientContext.Load(oListItemCollection);
clientContext.ExecuteQuery();
Console.WriteLine(“Id” + “\t” + “Name” + “\t” + “Age” + “\t” + “Code”);
foreach (ListItem oitem in oListItemCollection)
{
Console.WriteLine(Convert.ToString(oitem[“ID”]).Trim() + “\t” + Convert.ToString(oitem[“Title”]).Trim() + “\t” + oitem[“Student_x0020_Age”] + “\t” + oitem[“Student_x0020_Code”]);
}
Console.WriteLine(“Enter Id to update”);
int id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Enter EStudent Name”);
string EmpName = Console.ReadLine();
Console.WriteLine(“Enter Student Age”);
string Age = Console.ReadLine();
Console.WriteLine(“Enter Student Code”);
string Code = Console.ReadLine();
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.GetItemById(id);
oListItem[“Title”] = EmpName;
oListItem[“Student_x0020_Age”] = Age;
oListItem[“Student_x0020_Code”] = Code;
oListItem.Update();
clientContext.ExecuteQuery();
Console.WriteLine(“Updated successfully”);
}
else if (optionvalue == 3)
{
CamlQuery oquery = new CamlQuery();
ListItemCollection oListItemCollection = oList.GetItems(oquery);
clientContext.Load(oListItemCollection);
clientContext.ExecuteQuery();
Console.WriteLine(“List of items as below”);
Console.WriteLine(“Id” + “\t” + “Name” + “\t” + “Age” + “\t” + “Code”);
foreach (ListItem oitem in oListItemCollection)
{
Console.WriteLine(Convert.ToString(oitem[“ID”]).Trim() + “\t” + Convert.ToString(oitem[“Title”]).Trim() + “\t” + oitem[“Student_x0020_Age”] + “\t” + oitem[“Student_x0020_Code”]);
}
}
Console.WriteLine(“Do you want to repeat the option Y/N?”);
string optionresult = Console.ReadLine();
if (optionresult == “Y”)
{
goto startconsole;
}
}
}

Output as shown below,

Enter Option 1,

Enter Option 3

Enter Option Y

Enter Option 2

Enter N to exit

Click below to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg9VJ4aFU07BD91QxkA

Social tagging: > > > > > > > >