We can perform crud operation on SharePoint list using four object model.
To perform above operation assume you have a SharePoint list “Employee” which contains one column i.e. EmployeeName.
Server Side Object Model
Create Item
Add one record into Employee list using below code.
- //Get the SP site
- using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
- {
- //Get the Web site
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- // If List not exist it will throw an error
- SPList oList = oWeb.Lists["Employee "];
- //OR
- // If List not exist it will return null value
- SPList oList = oWeb.Lists.TryGetList("Employee");
- SPListItem oListItem = oList.AddItem();
- oListItem["Title"] = "Mr";
- oListItem["EmployeeName"] = "Arvind Kushwaha";
- oListItem.Update();
- }
- }
Edit the record from Employee list whose ID=1 using below code.
- //Get the SP site
- using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
- {
- //Get the Web site
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- // If List not exist it will throw an error
- SPList oList = oWeb.Lists["Employee "];
- //OR
- // If List not exist it will return null value
- SPList oList = oWeb.Lists.TryGetList("Employee");
- // Here you can pass dynamic ID or Your ID
- SPListItem oListitem = oList.GetItemById(1);
- oListitem["EmployeeName"] = "Arvind";
- oListitem.Update();
- }
- }
Delete Item
Delete the record from Employee list whose ID=1 using below code.
- //Get the SP site
- using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
- {
- //Get the Web site
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- // If List not exist it will throw an error
- SPList oList = oWeb.Lists["Employee "];
- //OR
- // If List not exist it will return null value
- SPList oList = oWeb.Lists.TryGetList("Employee ");
- // Here you can pass dynamic ID or Your ID
- SPListItem oListitem = oList.GetItemById(1);
- oListitem.Delete();
- }
- }
Get all Item
Get all the item from employee list using below code
- //Get the SP site
- using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
- {
- //Get the Web site
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- // If List not exist it will throw an error
- SPList oList = oWeb.Lists["Employee "];
- //OR
- // If List not exist it will return null value
- SPList oList = oWeb.Lists.TryGetList("Employee ");
- if (oList != null)
- {
- SPListItemCollection oListItemColl = oList.Items;
- foreach (SPListItem oListItem in oListItemColl)
- {
- Console.WriteLine(oListItem["Title"] + "::" + oListItem["EmployeeName "]);
- }
- }
- }
- }
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
- //Get the SP site
- using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
- {
- //Get the Web site
- using (SPWeb oWeb = oSite.OpenWeb())
- {
- // If List not exist it will throw an error
- SPList oList = oWeb.Lists["Employee"];
- //OR
- // If List not exist it will return null value
- SPList oList = oWeb.Lists.TryGetList("Employee");
- // Create a SPQuery Object
- SPQuery query = new SPQuery();
- //Write the query (I suggest using U2U Query Bulider Tool)
- query.Query = @"< Where >< Eq >< FieldRef Name ='EmployeeName'/>
- < Value Type ='Text'>Arvind </ Value ></ Eq ></ Where>";
- //Get the Items using Query
- SPListItemCollection curItems = oList.GetItems(query);
- // Go through the resulting items
- foreach (SPListItem curItem in curItems)
- {
- Console.WriteLine(curItem["Title"] + "::" + curItem["EmployeeName"]);
- }
- }
- }
Client Side Object Model
Create Item
Add one record into Employee list using below code.
Add one record into Employee list using below code.
- //Get the site
- string siteUrl = "SiteURL";
- ClientContext clientContext = new ClientContext(siteUrl);
- // Get the List
- List oList = clientContext.Web.Lists.GetByTitle("Employee");
- ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
- ListItem oListItem = oList.AddItem(listCreationInformation);
- oListItem["Title"] = "Mr";
- oListItem["EmployeeName"] = "Arvind Kushwaha";
- oListItem.Update();
- clientContext.ExecuteQuery();
Update Item
Edit the record from Employee list whose ID=1 using below code.
- //Get the site
- string siteUrl = "SiteURL";
- ClientContext clientContext = new ClientContext(siteUrl);
- // Get the List
- List oList = clientContext.Web.Lists.GetByTitle("Employee");
- ListItem oListItem = oList.GetItemById(1);
- oListItem["Title"] = "Male";
- oListItem.Update();
- clientContext.ExecuteQuery();
Delete Item
Delete the record from Employee list whose ID=1 using below code.
- //Get the site
- string siteUrl = "SiteURL”;
- ClientContext clientContext = new ClientContext(siteUrl);
- // Get the List
- List oList = clientContext.Web.Lists.GetByTitle("Employee");
- //Pass your ID
- ListItem oListItem = oList.GetItemById(1);
- oListItem.DeleteObject();
- clientContext.ExecuteQuery();
Get all Item
Get all the item from employee list using below code
- //Get the site
- string siteUrl = "SiteURL";
- ClientContext clientContext = new ClientContext(siteUrl);
- // Get the List
- List oList = clientContext.Web.Lists.GetByTitle("Employee");
- CamlQuery query = new CamlQuery();
- query.ViewXml = "<View/>";
- ListItemCollection items = oList.GetItems(query);
- clientContext.Load(oList);
- clientContext.Load(items);
- clientContext.ExecuteQuery();
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
- //Get the site
- string siteUrl = "SiteURL";
- ClientContext clientContext = new ClientContext(siteUrl);
- // Get the List
- List oList = clientContext.Web.Lists.GetByTitle("Employee");
- CamlQuery query = new CamlQuery();
- query.ViewXml = @"<View>
- <Query>
- <Where>
- <Eq>
- <FieldRef Name='EmployeeName '/>
- <Value Type='Text'>Arvind Kushwaha</Value>
- </Eq>
- </Where>
- </Query>
- </View>";
- ListItemCollection listItems = oList.GetItems(query);
- clientContext.Load(listItems, items => items.Include(
- item => item["Id"],
- item => item["Title"],
- item => item["EmployeeName"]
- ));
- clientContext.ExecuteQuery();
JavaScript Object Model
Create Item

krishna kishorePosted Sep 27, 2021, 9:31 AM
Thanks for sharing very helpful ..:-)
SamuelPosted Dec 31, 2020, 12:20 PM
Thanks for you sharing, you saved many hours of my life.
Sumit KumarPosted Jul 14, 2020, 2:54 AM
This is very helpful article Arvind. thanks for the sharing....
Ramakrishnan RPosted Jan 14, 2020, 2:08 PM
One of the best blog in SP crud operation; Great
nehanshu koshtiPosted Aug 12, 2019, 7:43 AM
Awesome script thanks arvind
Piyush AgarwalPosted Mar 6, 2018, 12:06 PM
Awesome .... Everything at one place