CRUD Operation using Server object model in Console Application
20 Mar 2017
This Blog helps you to do Server Object model 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,
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/“; using (SPSite site = new SPSite(siteUrl)) // Site Collection { using (SPWeb web = site.OpenWeb()) // Site { SPList list = web.Lists[“Student Details”]; // List SPQuery curQry = new SPQuery(); curQry.Query = “”; 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(); SPListItem oListItem = list.Items.Add(); oListItem[“Title”] = EmpName; oListItem[“Student_x0020_Age”] = Age; oListItem[“Student_x0020_Code”] = Code; oListItem.Update(); Console.WriteLine(“Inserted successfully”); } else if (optionvalue == 2) { //Get the Items using Query SPListItemCollection olistitemcollection = list.GetItems(curQry); Console.WriteLine(“Id” + “\t” + “Name” + “\t” + “Age” + “\t” + “Code”); foreach (SPListItem 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(); SPListItem oListItem = list.Items.GetItemById(id); oListItem[“Title”] = EmpName; oListItem[“Student_x0020_Age”] = Age; oListItem[“Student_x0020_Code”] = Code; oListItem.Update(); Console.WriteLine(“Updated successfully”); } else if (optionvalue == 3) { Console.WriteLine(“List of items as below”); SPListItemCollection olistitemcollection = list.GetItems(curQry); Console.WriteLine(“Id” + “\t” + “Name” + “\t” + “Age” + “\t” + “Code”); foreach (SPListItem 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 3
Enter Y
Enter Option 2
Enter Y and enter Option 1
Click below link to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg9VMr5BmgrwH2-FzMA
Recent Posts
Categories
- All
- Angularjs training in Chennai
- ASP.NET Core
- dot net training
- dot net training in chennai
- dotnet full stack developer
- Free dotnet training
- information on dotnet
- Learn Java in chennai
- Learn Python at Karaikudi
- learn python online
- learn python online from chennai
- Linq Queries in .net
- mutual funds
- MVC Training Tutorials
- PHP Training in Chennai
- pmp training online
- power apps online training
- Python Training Online
- share market
- Sharepoint framework online training
- SharePoint Freelancers in Chennai
- software testing
- spfx online training
- Stock market
- Uncategorized