SharePoint 2013 REST API Basic Operations

The main advantage of REST call is you can access the data through the end points from any Application which supports REST.

Some end points are mentioned below for SharePoint 2013.

URL endpoint Description Supported HTTP Method
/_api/Web/Lists Retrieving all the lists in a site and adding new lists. GET, POST
/_api/Web/Lists/GetByTitle('listname') Getting a list details by its title and updating it as well. If anyone 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 the 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 the 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


Here, we are going to see the basic and highly used REST calls in SharePoint 2013. Below are the functions, which we are going to learn today.

  1. Get List Items
  2. Adding New Item
  3. Update List Items
  4. Deleting Item

Get List Items

The function, mentioned below allows you to get the list items in regards to your query. You can filter the data in your query by choosing the suitable end points and pass the URL to the function.

(Make sure you have added the jQuery1.9.1.js and Microsoft AJAX JS file in your page).

  1. function getItems(url) {  
  2.     $.ajax({  
  3.         url: _spPageContextInfo.webAbsoluteUrl + url,  
  4.         type: "GET",  
  5.         headers: {  
  6.             "accept""application/json;odata=verbose",  
  7.         },  
  8.         success: function(data) {  
  9.             console.log(data.d.results);  
  10.         },  
  11.         error: function(error) {  
  12.             alert(JSON.stringify(error));  
  13.         }  
  14.     });  
  15. }  
For Example

To get items from all the fields, use the command, mentioned below.
var urlForAllItems = "/_api/Web/Lists/GetByTitle('ListName')/Items";

OR

To get items from only selected fields, use the command, mentioned below.
var urlForAllItems = "/_api/Web/Lists/GetByTitle('SpTutorial')/Items?$select=ID,Title";

Add New Item

As mentioned above, this function allows you to add the new item into the list. The difference between the two functions is the TYPE method. For retrieving the item, we will use “GET” method and for adding the new item, we will use “POST” method and we will pass JSON data to upload with the REST call.
  1. function addNewItem(url, data) {  
  2.     $.ajax({  
  3.         url: _spPageContextInfo.webAbsoluteUrl + url,  
  4.         type: "POST",  
  5.         headers: {  
  6.             "accept""application/json;odata=verbose",  
  7.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  8.             "content-Type""application/json;odata=verbose"  
  9.         },  
  10.         data: JSON.stringify(data),  
  11.         success: function(data) {  
  12.             console.log(data);  
  13.         },  
  14.         error: function(error) {  
  15.             alert(JSON.stringify(error));  
  16.         }  
  17.     });  
  18. }  
For Example
  1. var addNewItemUrl = "/_api/Web/Lists/GetByTitle('List Name')/Items";  
  2. var data = {  
  3.     __metadata: {  
  4.         'type''SP.Data.SpTutorialListItem'  
  5.     },  
  6.     Title: 'Some title',  
  7. };  
Update List Item

For updating the list item, we have three different methods in REST like PUT, PATCH and MERGE.
Here, PATCH and MERGE works same but the method PUT is different. If we need to update only one field in the list item, we will have to go for PATCH or MERGE because the PUT method will require all the fields in the data variable to update.

For example- we will consider the PATCH method to update the list item. The below methid allows you to update the list item. The item can be anything and it is all given by your URL parameter.
  1. function updateItem(url, oldItem, newItem) {  
  2.     $.ajax({  
  3.         url: _spPageContextInfo.webAbsoluteUrl + url,  
  4.         type: "PATCH",  
  5.         headers: {  
  6.             "accept""application/json;odata=verbose",  
  7.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  8.             "content-Type""application/json;odata=verbose",  
  9.             "X-Http-Method""PATCH",  
  10.             "If-Match": oldItem.__metadata.etag  
  11.         },  
  12.         data: JSON.stringify(newItem),  
  13.         success: function(data) {  
  14.             console.log(data);  
  15.         },  
  16.         error: function(error) {  
  17.             alert(JSON.stringify(error));  
  18.         }  
  19.     });  
  20. }  
End Point for the list item:
var updateItemUrl = "/_api/Web/Lists/GetByTitle('List Name')/getItemById('Id of old item')";

The parameter “newItem” in the function updateItem should be given in the proper JSON format, as we have done for adding new list item. Only the function will parse the data from JSON to patch.

Delete List Item

The delete operation is very easy, as we don’t need to pass any parameter except the endpoint. The method type “DELETE” will take care of everything, if it has the end point to delete.

Use the method, mentioned below to delete the list items. The function will delete the list items with respect to your end point URL
  1. function deleteItem(url, oldItem) {  
  2.     $.ajax({  
  3.         url: _spPageContextInfo.webAbsoluteUrl + url,  
  4.         type: "DELETE",  
  5.         headers: {  
  6.             "accept""application/json;odata=verbose",  
  7.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  8.             "If-Match": oldItem.__metadata.etag  
  9.         },  
  10.         success: function(data) {  
  11.   
  12.         },  
  13.         error: function(error) {  
  14.             alert(JSON.stringify(error));  
  15.         }  
  16.     });  
  17. }