We can perform crud operation on SharePoint list using four object model.

To perform above operation assume you have a SharePoint list Employeewhich contains one column i.e. EmployeeName.

Server Side Object Model

Create Item
Add one record into Employee list using below code.
  1. //Get the SP site
  2. using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
  3. {
  4. //Get the Web site
  5. using (SPWeb oWeb = oSite.OpenWeb())
  6. {
  7. // If List not exist it will throw an error
  8. SPList oList = oWeb.Lists["Employee "];
  9. //OR
  10. // If List not exist it will return null value
  11. SPList oList = oWeb.Lists.TryGetList("Employee");
  12. SPListItem oListItem = oList.AddItem();
  13. oListItem["Title"] = "Mr";
  14. oListItem["EmployeeName"] = "Arvind Kushwaha";
  15. oListItem.Update();
  16. }
  17. }
Update Item
Edit the record from Employee list whose ID=1 using below code.
  1. //Get the SP site
  2. using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
  3. {
  4. //Get the Web site
  5. using (SPWeb oWeb = oSite.OpenWeb())
  6. {
  7. // If List not exist it will throw an error
  8. SPList oList = oWeb.Lists["Employee "];
  9. //OR
  10. // If List not exist it will return null value
  11. SPList oList = oWeb.Lists.TryGetList("Employee");
  12. // Here you can pass dynamic ID or Your ID
  13. SPListItem oListitem = oList.GetItemById(1);
  14. oListitem["EmployeeName"] = "Arvind";
  15. oListitem.Update();
  16. }
  17. }
Delete Item
Delete the record from Employee list whose ID=1 using below code.
  1. //Get the SP site
  2. using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
  3. {
  4. //Get the Web site
  5. using (SPWeb oWeb = oSite.OpenWeb())
  6. {
  7. // If List not exist it will throw an error
  8. SPList oList = oWeb.Lists["Employee "];
  9. //OR
  10. // If List not exist it will return null value
  11. SPList oList = oWeb.Lists.TryGetList("Employee ");
  12. // Here you can pass dynamic ID or Your ID
  13. SPListItem oListitem = oList.GetItemById(1);
  14. oListitem.Delete();
  15. }
  16. }
Get all Item
Get all the item from employee list using below code
  1. //Get the SP site
  2. using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
  3. {
  4. //Get the Web site
  5. using (SPWeb oWeb = oSite.OpenWeb())
  6. {
  7. // If List not exist it will throw an error
  8. SPList oList = oWeb.Lists["Employee "];
  9. //OR
  10. // If List not exist it will return null value
  11. SPList oList = oWeb.Lists.TryGetList("Employee ");
  12. if (oList != null)
  13. {
  14. SPListItemCollection oListItemColl = oList.Items;
  15. foreach (SPListItem oListItem in oListItemColl)
  16. {
  17. Console.WriteLine(oListItem["Title"] + "::" + oListItem["EmployeeName "]);
  18. }
  19. }
  20. }
  21. }
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
  1. //Get the SP site
  2. using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
  3. {
  4. //Get the Web site
  5. using (SPWeb oWeb = oSite.OpenWeb())
  6. {
  7. // If List not exist it will throw an error
  8. SPList oList = oWeb.Lists["Employee"];
  9. //OR
  10. // If List not exist it will return null value
  11. SPList oList = oWeb.Lists.TryGetList("Employee");
  12. // Create a SPQuery Object
  13. SPQuery query = new SPQuery();
  14. //Write the query (I suggest using U2U Query Bulider Tool)
  15. query.Query = @"< Where >< Eq >< FieldRef Name ='EmployeeName'/>
  16. < Value Type ='Text'>Arvind </ Value ></ Eq ></ Where>";
  17. //Get the Items using Query
  18. SPListItemCollection curItems = oList.GetItems(query);
  19. // Go through the resulting items
  20. foreach (SPListItem curItem in curItems)
  21. {
  22. Console.WriteLine(curItem["Title"] + "::" + curItem["EmployeeName"]);
  23. }
  24. }
  25. }

Client Side Object Model

Create Item
Add one record into Employee list using below code.
  1. //Get the site
  2. string siteUrl = "SiteURL";
  3. ClientContext clientContext = new ClientContext(siteUrl);
  4. // Get the List
  5. List oList = clientContext.Web.Lists.GetByTitle("Employee");
  6. ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
  7. ListItem oListItem = oList.AddItem(listCreationInformation);
  8. oListItem["Title"] = "Mr";
  9. oListItem["EmployeeName"] = "Arvind Kushwaha";
  10. oListItem.Update();
  11. clientContext.ExecuteQuery();
Update Item
Edit the record from Employee list whose ID=1 using below code.
  1. //Get the site
  2. string siteUrl = "SiteURL";
  3. ClientContext clientContext = new ClientContext(siteUrl);
  4. // Get the List
  5. List oList = clientContext.Web.Lists.GetByTitle("Employee");
  6. ListItem oListItem = oList.GetItemById(1);
  7. oListItem["Title"] = "Male";
  8. oListItem.Update();
  9. clientContext.ExecuteQuery();
Delete Item
Delete the record from Employee list whose ID=1 using below code.
  1. //Get the site
  2. string siteUrl = "SiteURL”;
  3. ClientContext clientContext = new ClientContext(siteUrl);
  4. // Get the List
  5. List oList = clientContext.Web.Lists.GetByTitle("Employee");
  6. //Pass your ID
  7. ListItem oListItem = oList.GetItemById(1);
  8. oListItem.DeleteObject();
  9. clientContext.ExecuteQuery();
Get all Item
Get all the item from employee list using below code
  1. //Get the site
  2. string siteUrl = "SiteURL";
  3. ClientContext clientContext = new ClientContext(siteUrl);
  4. // Get the List
  5. List oList = clientContext.Web.Lists.GetByTitle("Employee");
  6. CamlQuery query = new CamlQuery();
  7. query.ViewXml = "<View/>";
  8. ListItemCollection items = oList.GetItems(query);
  9. clientContext.Load(oList);
  10. clientContext.Load(items);
  11. clientContext.ExecuteQuery();
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
  1. //Get the site
  2. string siteUrl = "SiteURL";
  3. ClientContext clientContext = new ClientContext(siteUrl);
  4. // Get the List
  5. List oList = clientContext.Web.Lists.GetByTitle("Employee");
  6. CamlQuery query = new CamlQuery();
  7. query.ViewXml = @"<View>
  8. <Query>
  9. <Where>
  10. <Eq>
  11. <FieldRef Name='EmployeeName '/>
  12. <Value Type='Text'>Arvind Kushwaha</Value>
  13. </Eq>
  14. </Where>
  15. </Query>
  16. </View>";
  17. ListItemCollection listItems = oList.GetItems(query);
  18. clientContext.Load(listItems, items => items.Include(
  19. item => item["Id"],
  20. item => item["Title"],
  21. item => item["EmployeeName"]
  22. ));
  23. clientContext.ExecuteQuery();

JavaScript Object Model

Create Item
Add one record into Employee list using below code.
  1. //Get the Site
  2. var context=new SP.ClientContex("Your Site URL");
  3. // Get the Web
  4. var web=context.get_web();
  5. // Get the list based on the Title
  6. var list=web.get_lists().getByTitle("Employee");
  7. //Object for creating Item in the List
  8. var listCreationInformation = new SP.ListItemCreationInformation();
  9. var listItem = list.addItem(listCreationInformation);
  10. listItem.set_item("Title", $("#CategoryId").val());
  11. listItem.set_item("CategoryName", $("#CategoryName").val());
  12. listItem.update(); //Update the List Item
  13. ctx.load(listItem);
  14. //Execute the batch Asynchronously
  15. ctx.executeQueryAsync(
  16. Function.createDelegate(this, success),
  17. Function.createDelegate(this, fail)
  18. );

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.
  1. //Get the site
  2. var contex = new SP.ClientContext("Your Site URL");
  3. //Get the web
  4. var web = contex.get_web();
  5. //Get the List based upon the Title
  6. var list = web.get_lists().getByTitle("Employee");
  7. ctx.load(list);
  8. listItem = list.getItemById(1);
  9. ctx.load(listItem);
  10. listItem.set_item("EmployeeName", "Arvind Kushwaha");
  11. listItem.update();
  12. ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));
The above code perform the Update the ListItem based upon the id,
Delete Item
Delete the record from Employee list whose ID=1 using below code.
  1. //Get the site
  2. var contex = new SP.ClientContext("Your Site URL");
  3. //Get the web
  4. var web = contex.get_web();
  5. //Get the List based upon the Title
  6. var list = web.get_lists().getByTitle("Employee");
  7. ctx.load(list);
  8. listItem = list.getItemById(1);
  9. ctx.load(listItem);
  10. listItem.deleteObject();
  11. ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));
The above code perform the Delete the ListItem based upon the id
Get all Item
Get all the item from employee list using below code
  1. //Get the site
  2. var contex = new SP.ClientContext("Your Site URL");
  3. //Get the web
  4. var web = contex.get_web();
  5. //Get the List based upon the Title
  6. var list = web.get_lists().getByTitle("Employee");
  7. //The Query object. This is used to query for data in the List
  8. var query = new SP.CamlQuery(); ctx.load(list);
  9. query.set_viewXml('<View></View>');
  10. var items = list.getItems(query);
  11. //Retrieves the properties of a client object from the server.
  12. ctx.load(list);
  13. ctx.load(items);
  14. ctx.executeQueryAsync(
  15. Function.createDelegate(this, function () {
  16. var enumerator = items.getEnumerator();
  17. while (enumerator.moveNext()) {
  18. var currentListItem = enumerator.get_current();
  19. alert(currentListItem.get_item("ID"));
  20. alert(currentListItem.get_item("Title"));
  21. alert(currentListItem.get_item("EmployeeName"));
  22. }
  23. }),
  24. Function.createDelegate(this, fail)
  25. );

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

  1. //Get the site
  2. var contex = new SP.ClientContext("Your Site URL");
  3. //Get the web
  4. var web = contex.get_web();
  5. //Get the List based upon the Title
  6. var list = web.get_lists().getByTitle("Employee");
  7. //The Query object. This is used to query for data in the List
  8. var query = new SP.CamlQuery(); ctx.load(list);
  9. //Create the CAML that will return only items with the titles that begin with 'A'
  10. query.set_viewXml('<View><Query><Where><BeginsWith><FieldRef Name="EmployeeName" /><Value Type="Text">A</Value></BeginsWith></Where></Query></View>');
  11. var items = list.getItems(query);
  12. //Retrieves the properties of a client object from the server.
  13. ctx.load(list);
  14. ctx.load(items);
  15. ctx.executeQueryAsync(
  16. Function.createDelegate(this, function () {
  17. //Get an enumerator for the items in the list
  18. var enumerator = items.getEnumerator();
  19. while (enumerator.moveNext()) {
  20. var currentListItem = enumerator.get_current();
  21. alert(currentListItem.get_item("ID"));
  22. alert(currentListItem.get_item("Title"));
  23. alert(currentListItem.get_item("EmployeeName"));
  24. }
  25. }),
  26. Function.createDelegate(this, fail)
  27. );

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.
  1. // Declare the variable.
  2. var listname="Employee",
  3. url=_spPageContextInfo.webAbsoluteUrl;
  4. // Preparing our update
  5. var item = $.extend({
  6. "__metadata": { "type": getListItemType(listname)}
  7. }, metadata);
  8. item.Title="MR";
  9. item.EmployeeName="Arvind Kushwaha""
  10. // Executing our adding operation
  11. $.ajax({
  12. url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
  13. type: "POST",
  14. contentType: "application/json;odata=verbose",
  15. data: JSON.stringify(item),
  16. headers: {
  17. "Accept": "application/json;odata=verbose",
  18. "X-RequestDigest": $("#__REQUESTDIGEST").val(),
  19. "Content-Type":"application/json;odata=verbose",
  20. "X-HTTP-Method": "POST"
  21. },
  22. success: function (data) {
  23. success(data); // Returns the newly created list item information
  24. },
  25. error: function (data) {
  26. failure(data);
  27. }
  28. });
Update Item
Edit the record from Employee list whose ID=1 using below code.
  1. // Declare the variable.
  2. var listname="Employee",
  3. id=1,
  4. url=_spPageContextInfo.webAbsoluteUrl;
  5. var item = $.extend({
  6. "__metadata": { "type": getListItemType(listname)}
  7. }, metadata);
  8. item.EmployeeName=’Arvind’;
  9. // Executing our Update operation based on ID
  10. $.ajax({
  11. url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(id)",
  12. type: "POST",
  13. contentType: "application/json;odata=verbose",
  14. data: JSON.stringify(item),
  15. headers: {
  16. "Accept": "application/json;odata=verbose",
  17. "X-RequestDigest": $("#__REQUESTDIGEST").val(),
  18. "Content-Type":"application/json;odata=verbose",
  19. "X-HTTP-Method": "MERGE"
  20. },
  21. success: function (data) {
  22. success(data); // Returns the newly created list item information
  23. },
  24. error: function (data) {
  25. failure(data);
  26. }
  27. });
Delete Item
Delete the record from Employee list whose ID=1 using below code.
  1. // Declare the variable.
  2. var listname="Employee",
  3. id=1,
  4. url=_spPageContextInfo.webAbsoluteUrl;
  5. // Executing our delete operation based on ID
  6. $.ajax({
  7. url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(id)",
  8. type: "POST",
  9. contentType: "application/json;odata=verbose",
  10. headers: {
  11. "Accept": "application/json;odata=verbose",
  12. "X-RequestDigest": $("#__REQUESTDIGEST").val(),
  13. "Content-Type":"application/json;odata=verbose",
  14. "X-HTTP-Method": "DELETE"
  15. },
  16. success: function (data) {
  17. success(data); // Returns the newly created list item information
  18. },
  19. error: function (data) {
  20. failure(data);
  21. }
  22. });
Get all Item
Get all the item from employee list using below code
  1. // Declare the variable.
  2. var url = _spPageContextInfo.webAbsoluteUrl;
  3. listname="Employee";
  4. // Retrieve all the item
  5. $.ajax({
  6. url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
  7. method: "GET",
  8. headers: { "Accept": "application/json; odata=verbose" },
  9. success: function (data) {
  10. // Returning the results
  11. console.log(data.d.results);
  12. },
  13. error: function (data) {
  14. failure(data);
  15. }
  16. });
Get Specific Item
Get all matching record from employee list where EmployeeName='Arvind' using below code
  1. // Declare the variable
  2. var url = _spPageContextInfo.webAbsoluteUrl;
  3. // Retrieve the item Title and Employee Name
  4. $.ajax({
  5. url: url + "/_api/web/lists/getbytitle('listname')/Items/?$select=Title,EmployeeName?$filterEmployeeName eq 'Arvind'",
  6. method: "GET",
  7. headers: { "Accept": "application/json; odata=verbose" },
  8. success: function (data) {
  9. // Returning the results
  10. console.log(data.d.results);
  11. },
  12. error: function (data) {
  13. failure(data);
  14. }
  15. });

Note

  • $select: Which column to retrieve in result.
  • $filter: What should be retrieve in result.
  • $expand: Retrieve the lookup column in result