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
Add one record into Employee list using below code.
- //Get the Site
- var context=new SP.ClientContex("Your Site URL");
- // Get the Web
- var web=context.get_web();
- // Get the list based on the Title
- var list=web.get_lists().getByTitle("Employee");
- //Object for creating Item in the List
- var listCreationInformation = new SP.ListItemCreationInformation();
- var listItem = list.addItem(listCreationInformation);
- listItem.set_item("Title", $("#CategoryId").val());
- listItem.set_item("CategoryName", $("#CategoryName").val());
- listItem.update(); //Update the List Item
- ctx.load(listItem);
- //Execute the batch Asynchronously
- ctx.executeQueryAsync(
- Function.createDelegate(this, success),
- Function.createDelegate(this, fail)
- );
The above code perform the following operations.
- To add a new item in the list, the SP.ListCreationInformation() object is used
- This object is then passed to the addItem() method of the List. This method returns the ListItem object
- Using the set_item() method of the ListItem the values for each field in the List is set and finally the list is updated.
Update Item
Edit the record from Employee list whose ID=1 using below code.
- //Get the site
- var contex = new SP.ClientContext("Your Site URL");
- //Get the web
- var web = contex.get_web();
- //Get the List based upon the Title
- var list = web.get_lists().getByTitle("Employee");
- ctx.load(list);
- listItem = list.getItemById(1);
- ctx.load(listItem);
- listItem.set_item("EmployeeName", "Arvind Kushwaha");
- listItem.update();
- ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));
Delete Item
Delete the record from Employee list whose ID=1 using below code.
- //Get the site
- var contex = new SP.ClientContext("Your Site URL");
- //Get the web
- var web = contex.get_web();
- //Get the List based upon the Title
- var list = web.get_lists().getByTitle("Employee");
- ctx.load(list);
- listItem = list.getItemById(1);
- ctx.load(listItem);
- listItem.deleteObject();
- ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));
Get all Item
Get all the item from employee list using below code
- //Get the site
- var contex = new SP.ClientContext("Your Site URL");
- //Get the web
- var web = contex.get_web();
- //Get the List based upon the Title
- var list = web.get_lists().getByTitle("Employee");
- //The Query object. This is used to query for data in the List
- var query = new SP.CamlQuery(); ctx.load(list);
- query.set_viewXml('<View></View>');
- var items = list.getItems(query);
- //Retrieves the properties of a client object from the server.
- ctx.load(list);
- ctx.load(items);
- ctx.executeQueryAsync(
- Function.createDelegate(this, function () {
- var enumerator = items.getEnumerator();
- while (enumerator.moveNext()) {
- var currentListItem = enumerator.get_current();
- alert(currentListItem.get_item("ID"));
- alert(currentListItem.get_item("Title"));
- alert(currentListItem.get_item("EmployeeName"));
- }
- }),
- Function.createDelegate(this, fail)
- );
The above code performs the following operations,
- Use SP.CamlQuery() to create query object for querying the List
- The query object is set with the criteria using xml expression using set_viewXml() method
- Using getItems() method of the List the query will be processed
- executeQueryAsync() methods processes the batch on the server and retrieve the List data. This data is displayed using HTML table after performing iterations on the retrieved data
Get Specific Item
- //Get the site
- var contex = new SP.ClientContext("Your Site URL");
- //Get the web
- var web = contex.get_web();
- //Get the List based upon the Title
- var list = web.get_lists().getByTitle("Employee");
- //The Query object. This is used to query for data in the List
- var query = new SP.CamlQuery(); ctx.load(list);
- //Create the CAML that will return only items with the titles that begin with 'A'
- query.set_viewXml('<View><Query><Where><BeginsWith><FieldRef Name="EmployeeName" /><Value Type="Text">A</Value></BeginsWith></Where></Query></View>');
- var items = list.getItems(query);
- //Retrieves the properties of a client object from the server.
- ctx.load(list);
- ctx.load(items);
- ctx.executeQueryAsync(
- Function.createDelegate(this, function () {
- //Get an enumerator for the items in the list
- var enumerator = items.getEnumerator();
- while (enumerator.moveNext()) {
- var currentListItem = enumerator.get_current();
- alert(currentListItem.get_item("ID"));
- alert(currentListItem.get_item("Title"));
- alert(currentListItem.get_item("EmployeeName"));
- }
- }),
- Function.createDelegate(this, fail)
- );
The above code performs the following operations:
- Use SP.CamlQuery() to create query object for querying the List
- The query object is set with the criteria using xml expression using set_viewXml() method
- Using getItems() method of the List the query will be processed
- executeQueryAsync() methods processes the batch on the server and retrieve the List data. This data is displayed using HTML table after performing iterations on the retrieved data
REST-API Object Model.
Create Item
Add one record into Employee list using below code.
- // Declare the variable.
- var listname="Employee",
- url=_spPageContextInfo.webAbsoluteUrl;
- // Preparing our update
- var item = $.extend({
- "__metadata": { "type": getListItemType(listname)}
- }, metadata);
- item.Title="MR";
- item.EmployeeName="Arvind Kushwaha""
- // Executing our adding operation
- $.ajax({
- url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
- type: "POST",
- contentType: "application/json;odata=verbose",
- data: JSON.stringify(item),
- headers: {
- "Accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "Content-Type":"application/json;odata=verbose",
- "X-HTTP-Method": "POST"
- },
- success: function (data) {
- success(data); // Returns the newly created list item information
- },
- error: function (data) {
- failure(data);
- }
- });
Update Item
Edit the record from Employee list whose ID=1 using below code.
- // Declare the variable.
- var listname="Employee",
- id=1,
- url=_spPageContextInfo.webAbsoluteUrl;
- var item = $.extend({
- "__metadata": { "type": getListItemType(listname)}
- }, metadata);
- item.EmployeeName=’Arvind’;
- // Executing our Update operation based on ID
- $.ajax({
- url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(id)",
- type: "POST",
- contentType: "application/json;odata=verbose",
- data: JSON.stringify(item),
- headers: {
- "Accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "Content-Type":"application/json;odata=verbose",
- "X-HTTP-Method": "MERGE"
- },
- success: function (data) {
- success(data); // Returns the newly created list item information
- },
- error: function (data) {
- failure(data);
- }
- });
Delete Item
Delete the record from Employee list whose ID=1 using below code.
- // Declare the variable.
- var listname="Employee",
- id=1,
- url=_spPageContextInfo.webAbsoluteUrl;
- // Executing our delete operation based on ID
- $.ajax({
- url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(id)",
- type: "POST",
- contentType: "application/json;odata=verbose",
- headers: {
- "Accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "Content-Type":"application/json;odata=verbose",
- "X-HTTP-Method": "DELETE"
- },
- success: function (data) {
- success(data); // Returns the newly created list item information
- },
- error: function (data) {
- failure(data);
- }
- });
Get all Item
Get all the item from employee list using below code
- // Declare the variable.
- var url = _spPageContextInfo.webAbsoluteUrl;
- listname="Employee";
- // Retrieve all the item
- $.ajax({
- url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: function (data) {
- // Returning the results
- console.log(data.d.results);
- },
- error: function (data) {
- failure(data);
- }
- });
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
- // Declare the variable
- var url = _spPageContextInfo.webAbsoluteUrl;
- // Retrieve the item Title and Employee Name
- $.ajax({
- url: url + "/_api/web/lists/getbytitle('listname')/Items/?$select=Title,EmployeeName?$filterEmployeeName eq 'Arvind'",
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: function (data) {
- // Returning the results
- console.log(data.d.results);
- },
- error: function (data) {
- failure(data);
- }
- });
Note
- $select: Which column to retrieve in result.
- $filter: What should be retrieve in result.
- $expand: Retrieve the lookup column in result

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