Share via


SharePoint 2013: List Items CRUD Operation Using REST API (Part 3)

Before reading this article, please go through the following article series:

**Introduction **

SharePoint 2013 has greatly expanded the REST services available to developers. With this, we have much more SharePoint functionality exposed via JSOM and Web Services. The goal of this article is to provide how to perform basic create, read, update, and delete (CRUD) operations on lists and list items with the REST services.

SharePoint REST endpoint Overview:

The following table contains typical REST endpoint URL examples to get you started working with SharePoint data. Prepend http://server/site/_api/ to the URL fragments shown in the table to construct a fully qualified REST URL. Below is a list of the basic commands used to get List Items from a SharePoint List through the SharePoint 2013 REST Services.

URL endpoint Description Supported HTTP Method
/_api/Web/Lists/ getbytitle('listname') Getting a list details by its title and updating it as well. Ifanyone changes your list title, your code will break. GET, POST
/_api/Web/Lists(guid'guid id of your list') Same as above but changing list title will not affect the code. GET, POST
/_api/Web/Lists/getbytitle(' listname ')/Fields Retrieving all fields associated with a list and add new fields GET, POST
/_api/Web/Lists/getbytitle('listname')/

Fields/getbytitle('fieldname')

Getting details of a field, modifying and deleting it. GET, PUT, PATCH, MERGE, DELETE
/_api/Web/Lists/getbytitle('listname')

/Items

Retrieving all items in a list and adding new items GET, POST
/_api/web/lists/getbytitle('listname')

/GetItemById(itemId) 

 

This endpoint can be used to get, update and delete a single item. GET, PUT, PATCH, MERGE, DELETE
/_api/lists/ getbytitle (‘'listname')/items?$orderby=Title Order Your Results GET, POST
/_api/lists/ getbytitle (‘'listname')/items?$select=Title,Id Retrieve Selected Column Data value GET, POST
/_api/web/lists/getbytitle('listname')/Items/

?$select=Title,FieldName/Id&$expand= FieldName /Id

Retrieving the lookup value GET, POST

Now, we will demo all the operations on list items, including retrieve, create, update and delete on list items.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/crud-operation-on-list-items-using-rest-api-services-in-shar/Images/image002.jpg

Retrieve the list items

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/crud-operation-on-list-items-using-rest-api-services-in-shar/Images/image003.png

Main code in detail:

  1. function retriveListItem() 
  2.  
  3.  $.ajax 
  4.  ({ 
  5.  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items?$select=Company,Industry", 
  6.  type: type, 
  7.  data: data, 
  8.  headers: 
  9.  { 
  10.   "Accept": "application/json;odata=verbose", 
  11.   "Content-Type": "application/json;odata=verbose", 
  12.   "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
  13.   "IF-MATCH": "*", 
  14.   "X-HTTP-Method": null 
  15.  }, 
  16.  cache: false, 
  17.  success: function(data) 
  18.  { 
  19.   $("#ResultDiv").empty(); 
  20.   for (var i = 0; i < data.d.results.length; i++) 
  21.   { 
  22.   var item = data.d.results[i]; 
  23.   $("#ResultDiv").append(item.Company + "\t" + item.Industry + "<br/>"); 
  24.   } 
  25.  }, 
  26.  error: function(data) 
  27.  { 
  28.   $("#ResultDiv").empty().text(data.responseJSON.error); 
  29.  } 
  30.  }); 

Create list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/crud-operation-on-list-items-using-rest-api-services-in-shar/Images/image004.png

Here is the main code in detail:

  1. function AddListItem() 
  2.  var industryVal = $("#Industry").val(); 
  3.  var Company = $("#Company").val(); 
  4.  $.ajax 
  5.  ({ 
  6.  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items", 
  7.  type: "POST", 
  8.  data: JSON.stringify 
  9.  ({ 
  10.   __metadata: 
  11.   { 
  12.   type: "SP.Data.TestListItem" 
  13.   }, 
  14.   Company: Company, 
  15.   Industry: industryVal 
  16.  }), 
  17.  headers: 
  18.  { 
  19.   "Accept": "application/json;odata=verbose", 
  20.   "Content-Type": "application/json;odata=verbose", 
  21.   "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
  22.   "X-HTTP-Method": "POST" 
  23.  }, 
  24.  success: function(data, status, xhr) 
  25.  { 
  26.   retriveListItem(); 
  27.  }, 
  28.  error: function(xhr, status, error) 
  29.  { 
  30.   $("#ResultDiv").empty().text(data.responseJSON.error); 
  31.  } 
  32.  }); 

Update list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/crud-operation-on-list-items-using-rest-api-services-in-shar/Images/image005.png

Here is the main code in detail:

  1. function updateListItem() 
  2.  var industryVal = $("#Industry").val(); 
  3.  $.ajax 
  4.  ({ 
  5.  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)", // list item ID 
  6.  type: "POST", 
  7.  data: JSON.stringify 
  8.  ({ 
  9.   __metadata: 
  10.   { 
  11.   type: "SP.Data.TestListItem" 
  12.   }, 
  13.   Industry: industryVal 
  14.  }), 
  15.  headers: 
  16.  { 
  17.   "Accept": "application/json;odata=verbose", 
  18.   "Content-Type": "application/json;odata=verbose", 
  19.   "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
  20.   "IF-MATCH": "*", 
  21.   "X-HTTP-Method": "MERGE" 
  22.  }, 
  23.  success: function(data, status, xhr) 
  24.  { 
  25.   retriveListItem(); 
  26.  }, 
  27.  error: function(xhr, status, error) 
  28.  { 
  29.   $("#ResultDiv").empty().text(data.responseJSON.error); 
  30.  } 
  31.  }); 

Delete list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/crud-operation-on-list-items-using-rest-api-services-in-shar/Images/image006.png

Here is the main code in detail:

  1. function deleteListItem() 
  2.  $.ajax 
  3.  ({ 
  4.  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)", 
  5.  type: "POST", 
  6.  headers: 
  7.  { 
  8.   "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
  9.   "IF-MATCH": "*", 
  10.   "X-HTTP-Method": "DELETE" 
  11.  }, 
  12.  success: function(data, status, xhr) 
  13.  { 
  14.   retriveListItem(); 
  15.  }, 
  16.  error: function(xhr, status, error) 
  17.  { 
  18.   $("#ResultDiv").empty().text(data.responseJSON.error); 
  19.  } 
  20.  }); 

Summary

In this article, we explored SharePoint 2013 REST API for (CRUD) operations on list items level. We’ve tried to explore CRUD operation using REST Services, JavaScript Client Side Object Model, and SOAP Services to work on the client side.

Credits

My Blog Reference