SharePoint Rest API – Complete Useful Tutorial

In this SharePoint tutorial, let us try to understand SharePoint rest api. We will see how to use rest api in SharePoint Online as well as we will see how to use rest api in SharePoint 2013/2016/2019. To work with rest api in SharePoint, we need to have a clear understanding of Rest API GET, POST, PUT, DELETE, and PATCH also. In this tutorial, I will also go through various SharePoint rest api examples. Here we will cover:

  • What is SharePoint Rest API?
  • Advantages of using Rest API in SharePoint
  • Understanding SharePoint Rest API HTTP commands
    • GET
    • POST
    • PUT/MERGE
    • DELETE
  • Various useful SharePoint REST endpoint examples
  • SharePoint rest api crud operations
    • SharePoint rest api get all list items
    • SharePoint rest api insert list item
    • SharePoint rest api update list item
    • SharePoint rest api delete list item
  • SharePoint rest api __metadata
  • How to use SharePoint rest api in a browser
  • Internet Explorer cannot display this feed
  • Access SharePoint Rest API using Postman

What is SharePoint Rest API?

What is SharePoint Rest API? Rest API stands for Representational State Transfer and is based on standard Open Data Protocol (OData). By using Rest API, we can interact with SharePoint remotely by using any technology that supports Rest protocol. By using SharePoint rest api, we can do various operations like create, update, delete and display operations, like the SharePoint rest api CRUD operations.

By using Rest API, we can do CRUD operations from SharePoint Add-ins, solutions, and from client object model applications also.

SharePoint Rest API is another form of client object model similar to JSOM, or CSOM.

Advantages of using Rest API in SharePoint

What are the advantages of using Rest API in SharePoint? One of the main advantages of SharePoint rest api is it allows you to work remotely with SharePoint sites using any technologies that support Rest protocols.

Another advantage of using Rest API is that you do not need to add any or refer to any SharePoint libraries or dlls to work with Rest API, you just need to make an HTTP rest endpoint (URL) for the SharePoint operations like insert, update or delete.

Understanding SharePoint Rest API HTTP commands

Now let us try to understand SharePoint rest api HTTP commands. If you want to work with Rest API in SharePoint Online or SharePoint 2013/2016/2019, the first things we need to construct a Restful HTTP request by using the OData protocol standard.

If you want to do create, read, update and delete operations using SharePoint Rest API, then we need to use the GET, POST, PUT, MERGE, DELETE and PATCH HTTP methods.

GET

In rest api we use the HTTP GET method if we want to read information from the SharePoint server or if you want to retrieve information from SharePoint list if you want to read items from the SharePoint list. So mostly for the Read operations we use the GET method.

POST

In SharePoint, if you want to create or update operations like creating a list or creating an item in a SharePoint Online list, then we use the Rest API POST operations. In Rest API POST operations, if any properties are not required then these are set to their default values.

PUT/MERGE

In SharePoint Online, if you want to update an existing object like an update item in a SharePoint list or update the SharePoint list title, then we can use the Rest API PUT and MERGE operations.

There is a little difference between HTTP PUT and MERGE in SharePoint rest api.

In the REST API MERGE HTTP method, setting properties is optional, and if any properties that you don’t explicitly set keep their current property.

For the PUT operations, it is mandatory to set up all the required properties while updating SharePoint objects. And for the optional properties, if you do not specify the values, then it will set to the default properties.

DELETE

As the name suggests, the HTTP DELETE method is used when we want to delete any SharePoint objects like deleting SharePoint list, list items, documents, etc.

Now, I hope you got an idea of which HTTP commands, we should use while working with SharePoint rest api CRUD operations.

Various useful SharePoint REST endpoint examples

For doing any kind of operation in SharePoint using Rest API, the first thing we need to do is to create the rest endpoint. All the rest endpoint URL starts with:

https://SiteURL/Sites/SiteName/_api/

Example:

Below is an example for SharePoint Online.

https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/

For on-premises versions like SharePoint 2013, SharePoint 2016, or SharePoint 2019, the Rest api endpoint will be like below:

http://bsahoo3:8787/sites/Training/_api/

Here are a various SharePoint rest api endpoint examples, I have taken a SharePoint Online site as reference, but the Rest API endpoints will be same for SharePoint on-premises.

But while working with Rest API code, we can get the site URL by using the below JavaScript variable:

_spPageContextInfo.webAbsoluteUrl

You can check out more on How to use _spPageContextInfo JavaScript variable in SharePoint.

Operation SharePoint Rest API endpoint
Get SharePoint site collection https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/site
Get specific SharePoint site or web https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web
Get SharePoint site title https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/title
Get all lists from a SharePoint site https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists
Get all items from a SharePoint list https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists/getbytitle(‘Trainings’)/items
Get SharePoint list title using Rest API https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists/getbytitle(‘Trainings’)?select=Title
Get all columns from a SharePoint list https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists/getbytitle(‘Trainings’)/Fields
Get SharePoint list by using a list GUIDs https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/Lists(List GUIDs)
Get SharePoint list item by item id https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists/GetByTitle(‘Trainings’)/GetItemById(2)
Get SharePoint logged in user information or current user information https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/currentUser
Get selected fields for SharePoint list items https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/web/lists/getbytitle(‘Trainings’)/Items?select=ID,Title,FirstName,LastName
Get all SharePoint site users https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/siteusers
Get all SharePoint groups from the site https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/sitegroups
Get particular SharePoint group by group id https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/sitegroups/GetById(GroupId)
Get all users from a SharePoint group https://tsinfotechnologies.sharepoint.com/sites/SPGuides/_api/Web/sitegroups(Id)/users
Rest API endpoint examples

SharePoint rest api crud operations

Now, let us see an example on SharePoint rest api crud operations. We will see how to create a list item in SharePoint Online using Rest API, display SharePoint list items using Rest API, update list item using SharePoint rest API, and how to delete an item from the SharePoint list using rest api.

So below are the example of crud operations in SharePoint rest api. The rest api code we can add in a script editor web part in a SharePoint Online web part page.

SharePoint rest api get all list items

Let us first see, how to get all list items using SharePoint rest api.

Here I have added a button and on button click, we can retrieve SharePoint list items using rest api and display them.

Here, you can see in the below code, I have taken a button and on click of that button, calling the GetListItems() method. And I am displaying all the SharePoint list items in a dialog box in the onSuccess method.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<button onclick="GetListItems();" type="button">Get All List Items​</button>

<script>
function GetListItems()
{
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employees')/items";
$.ajax({
url: url,
type: "GET",
headers:
 {
"accept":"application/json; odata=verbose"
},
success: onSuccess,error: onError
});
}

function onSuccess(data) {
var items = data.d.results;
var allItems='';
for (var i = 0; i < items.length; i++) {
allItems+="Item ID- "+ items[i].Id+ " : Title- " + items[i].Title + '\r\n';
}
alert(allItems);
}

function onError(error) {alert(JSON.stringify(error));
}
</script>

Save the above code in a script editor web part and then click on the button, you can see the output like below:

SharePoint rest api get all list items
SharePoint rest api get all list items

This is how we can get all list items in SharePoint Online using rest api.

SharePoint rest api insert list item

Now, in the next part of the SharePoint rest api CRUD operation, we will see an example of the SharePoint rest api insert list item.

We will insert an item to the same SharePoint Online list. Here also, I have added button control and once the user clicks on the button, the item will be inserted into the list using Rest API code.

Here, we have added a few new things:

POST: Here, it is going to be a HTTP POST request because it is an insert operation. For any kind of insert or update operation, we have use the HTTP POST request.

__REQUESTDIGEST: While doing any rest api POST operation, we need to send a valid request digest to the SharePoint page. This is a token and it helps SharePoint to understand that the request is a valid request.

SharePoint always includes a request digest token on every page in a hidden field as __REQUESTDIGEST. So while working with Rest API, we need to get this hidden value from the field and send it with the request in a parameter as X-RequestDigest. The token valid for 30 minutes. It will be like below:

"X-RequestDigest": $("#__REQUESTDIGEST").val()

It is must for any kind of SharePoint rest api POST request.

Another important is the type which we do like below:

type: "SP.Data.EmployeesListItem"

Ideally, you should follow below method to get it.

'SP.Data.' + 'List Internal Name' + 'ListItem'

Example: SP.Data.' + 'Employees' + 'ListItem' = SP.Data.EmployeesListItem

Below is the complete code to insert item to SharePoint using rest api.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<button onclick="InsertItem();" type="button">Insert Item to SharePoint List​</button>

<script>
function InsertItem()
{
var restendpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employees')/items";
$.ajax
({
url: restendpoint,
type: "POST",
data: JSON.stringify
(
{
__metadata:
{
type: "SP.Data.EmployeesListItem"
},
Title : "Bijay Kumar"
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function()
{
alert("Item added to the SharePoint list");
},
error: function()
{
alert("Error Occurred!");
}
});
}
</script>

Once you run the code and click on the button, you can see the item will be inserted to the SharePoint list.

insert item to SharePoint using rest api
insert item to SharePoint using rest api

SharePoint rest api update list item

Now, let us see another example on SharePoint rest api update list item. Updating an item is almost similar to inserting an item to the SharePoint list, the only difference is the rest endpoint.

We need to get the item by id and then we can update.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<button onclick="UpdateItem();" type="button">Update Item (ID=4)​</button>

<script>
function UpdateItem()
{
var restendpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employees')/items/getbyid(4)";

$.ajax({  
    url: restendpoint,  
    type: "POST",  
    headers: {  
        "accept": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "content-Type": "application/json;odata=verbose",  
        "IF-MATCH": "*",  
        "X-HTTP-Method": "MERGE"  
    },  
    data: "{__metadata:{'type':'SP.Data.EmployeesListItem'},Title:'Bhawana Rathore'}",
    success: function(data) {  
        alert('Item updated successfully!');
    },  
    error: function(error) {  
        alert(JSON.stringify(error));  
    }  
});  

}
</script>

Once you save the code and click on the button, it update the list item whose id=5.

sharepoint rest api update list item
sharepoint rest api update list item

Here is another example on SharePoint rest api update list item by id.

SharePoint rest api delete list item

Now, let us see another example of SharePoint crud operations using rest api on SharePoint rest api delete list item.

This is also going to be a POST operation but we have to pass additional parameter as “X-Http-Method”: “DELETE”.

The rest endpoint is the same as updating list item, because we will first get the item by id which item you want to delete.

Below is the complete code to delete SharePoint list item using Rest API.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<button onclick="DeleteItem();" type="button">Delete Item (ID=3)​</button>

<script>
function DeleteItem()
{
var restendpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Employees')/items/getbyid(3)";

$.ajax({  
    url: restendpoint,  
    type: "POST",    
    headers: {  
        "Accept": "application/json;odata=verbose",  
        "X-Http-Method": "DELETE",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "If-Match": "*"  
    },  
    success: function(data) {  
        alert('SharePoint list item deleted');  
    },  
    error: function(data) {  
        alert('Error Occurred!');  
    }
});  

}
</script>
sharepoint rest api delete list item
sharepoint rest api delete list item

So this is an example on delete list item in SharePoint using Rest API.

I hope you liked this SharePoint rest api crud operations examples.

SharePoint rest api __metadata

Let us see, the SharePoint rest api __metadata used for what purpose?

__metadata is very important while working with SharePoint rest api, especially while doing any HTTP POST operations like insert and update.

Through __metadata we provide various information required to create or update operations. And another important property we set is the type parameter.

For example, if you want to create a list in the SharePoint Online site by using Rest API, then we need to pass type=SP.List and other information like the list Title, Description, BaseTemplate, etc. It looks like below:

data:  JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,
 'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': 'This is the list description', 'Title': 'This is List Title' })

Similarly if you want to create a site in SharePoint using Rest API, we have to pass the type= SP.WebInfoCreationInformation and also we need to pass other data required to create a site like Title, Description, Url, WebTemplate, Language, etc. It should looks like below:

data: JSON.stringify(
        {'parameters': {
            '__metadata':  {'type': 'SP.WebInfoCreationInformation' },
            'Url': 'SiteURL',
            'Title': 'Site Title',
            'Description': 'Site Description,
            'Language':1033,
            'WebTemplate':'sts',
            'UseUniquePermissions':false}
        }
    )

This is why we use SharePoint rest api __metadata.

How to use SharePoint rest api in a browser

Let us see now how to use SharePoint rest api in a browser?

We can put the rest endpoint in the browser and we can the result. Let us check what will happen when you put the rest endpoint in the browser.

https://tsinfotechnologies.sharepoint.com/sites/SPGuidesClassic/_api/web/lists/getByTitle('Employees')/items/getbyid(7)

Once you enter the URL, if you have not signed in then it will give you error message like Access denied. If you have already logged in, then you can see the output like below in XML.

how to use sharepoint rest api in a browser
how to use sharepoint rest api in a browser

If you are trying to open the same SharePoint rest api endpoint in the Internet explorer, you can see the output will come like below:

how to use sharepoint online rest api in a browser
how to use sharepoint online rest api in a browser

Internet Explorer cannot display this feed

If the output did come properly in IE browser and you see an error like Internet Explorer cannot display this feed.

Internet Explorer cannot display this feed
Internet Explorer cannot display this feed

Follow the below steps to solve the issue.

Open Internet Explorer, click on Settings icon -> Internet Options and then click on Content -> click on Settings that is under Feeds and Web Slices. This will open the Feed and Web Slice Settings page, there uncheckTurn on feed reading view” like below and then click on OK to save the changes.

Internet explorer cannot display this feed rest api
Internet explorer cannot display this feed rest api

Now, when you put the rest api endpoint, you can see the data in properly in XML format like below:

Internet explorer cannot display this feed
Internet explorer cannot display this feed

This way, we can fix Internet explorer cannot display this feed error that comes while try to access SharePoint rest api in a browser.

Apart from this in Internet Explorer, you can append view source: into the rest api URL to see more information like below:

view-source:https://tsinfotechnologies.sharepoint.com/sites/SPGuidesClassic/_api/web/lists/getByTitle('Employees')/items/getbyid(7)

Access SharePoint Rest API using Postman

Apart from this, we can also access SharePoint rest api using Postman in the Google Chrome browser. Bhawana has written an article on the same you can have a look Access Rest API using Postman in SharePoint Online/2013/2016.

SharePoint Rest API CRUD Operations – Video Tutorial

Here is the video tutorial on SharePoint rest api crud operations. Subscribe to Our YouTube Channel for more FREE videos.

You may like the following SharePoint rest api tutorials:

In this tutorial, we discussed on SharePoint rest api and the below things:

  • What is SharePoint Rest API?
  • Advantages of using Rest API in SharePoint
  • Understanding SharePoint Rest API HTTP commands
    • GET
    • POST
    • PUT/MERGE
    • DELETE
  • Various useful SharePoint REST endpoint examples
  • SharePoint rest api crud operations
  • SharePoint rest api get all list items
  • SharePoint rest api insert list item
  • SharePoint rest api update list item
  • SharePoint rest api delete list item
  • SharePoint rest api __metadata
  • How to use SharePoint rest api in a browser
  • Internet Explorer cannot display this feed
  • Access SharePoint Rest API using Postman
  • >