SharePoint Rest API Tutorial and Examples

SharePoint rest api tutorial and examples

This SharePoint tutorial, we will discuss on Rest API in SharePoint, advantages of Rest API in SharePoint. Also we will discuss various properties of Rest API and few examples of Rest API in SharePoint.

The Rest API code whatever we will use in this tutorial, will work in SharePoint Online as well as SharePoint 2013/2016/2019.

SharePoint deveopment training course

SharePoint Rest API

We worked on various client object model techniques like CSOM, JSOM, etc. Microsoft also in introduced REST (Representational State Transfer) service in SharePoint 2013 which is comparable to existing SharePoint client object models.

By using Rest API, we can interact with SharePoint remotely. We can interact to with SharePoint data by using any technology that supports REST web requests like you can perform create, read, update and delete operations from apps for SharePoint, client applications, etc. using REST technologies.

In SharePoint 2013/2016/Online you can interact remotely with SharePoint sites using REST API. You will be able to interact by using any technology that supports REST web requests. You need to construct a RESTful HTTP request, using the Open Data Protocol (OData) standard.

To access SharePoint resources using REST, construct a RESTful HTTP request, using the Open Data Protocol (OData) standard, which corresponds to the desired client object model API.

Example:
Here is below the client object model and REST:

Client object model method:
List.GetByTitle(listname)

REST endpoint:
http://server/site/_api/lists/getbytitle(‘listname’)

SharePoint rest api tutorial and examples

Here your application will send an HTTP request and client.svc web service handles the request which internally calls the Server object model to retrieve data from the Content database. And then it sends the response in either Atom or JavaScript Object Notation (JSON) format which your client application should able to parse.

REST is a software architecture that uses uniform resource identifiers (URIs) to specify operations against a remote service. Open Data Protocol (OData) is the protocol that is used along with REST to access many cloud-based services.

Suppose in the client object model we write List.GetByTitle(listname) to retrieve a particular list. The same thing if you want to do in REST then it will be like:

http://server/site/_api/lists/getbytitle('listname')

This is known as REST endpoint.

If you want to read data from the SharePoint site then you should use HTTP GET request.

Similarly, if you want to add or update then you should use POST request.

If you want to Delete then you should do a HTTP DELETE request.

HTTP operations in SharePoint 2013 REST services

The endpoints in the SharePoint 2013 REST service correspond to the types and members in the SharePoint client object models. By using HTTP requests, you can use these REST endpoints to perform typical CRUD (Create, Read, Update, and Delete) operations against SharePoint entities, such as lists and sites.

Typically, endpoints that represent Read operations map to HTTP GET commands. Endpoints that represent update operations map to HTTP POST commands, and endpoints that represent update or insert operations map to HTTP PUT commands.

In SharePoint 2013, use POST to create entities such as lists and sites. The SharePoint 2013 REST service supports sending POST commands that include object definitions to endpoints that represent collections. For example, you could send a POST command that included a new list object definition in ATOM to the following URL, to create a SharePoint list:

http://<site url>/_api/web/lists

For POST operations, any properties that are not required are set to their default values. If you attempt to set a read-only property as part of a POST operation, the service returns an exception.

Use PUT and MERGE operations to update existing SharePoint objects. Any service endpoint that represents an object property set operation supports both PUT requests and MERGE requests.

For MERGE requests, setting properties is optional; any properties that you do not explicitly set retain their current property.

For PUT commands, however, any properties you do not explicitly set are set to their default properties. In addition, if you do not specify all required properties in object updates when using HTTP PUT commands, the REST service returns an exception.

Use the HTTP DELETE command against the specific endpoint URL to delete the SharePoint object represented by that endpoint. In the case of recyclable objects, such as lists, files, and list items, this results in a Recycle operation.

To access a specific site collection, you can use like below:
http://server/site/_api/site

To access a specific site, you can use like below:
http://server/site/_api/web

Few SharePoint 2013 REST Service Endpoints

Here are a few rest endpoints for SharePoint 2013/2016/Online. The REST interface exposes all of the SharePoint entities and operations that are available in the other SharePoint client APIs. One advantage of using REST is that you don’t have to add references to any SharePoint libraries or client assemblies. Instead, you make HTTP requests to the appropriate endpoints to retrieve or update SharePoint entities, such as webs, lists, and list items.

Here we will see the different areas, what is corresponding client object model, corresponding REST EndPoint, and the Server Object Model.

List
Client Object Model: ClientContext.Web.Lists
REST EndPoint: http://server/site/_api/web/lists
Server Object Model: SPList

Site
Client Object Model: ClientContext.Site
REST EndPoint: http://server/site/_api/site
Server Object Model: SPSite

Web
Client Object Model: ClientContext.Web
REST EndPoint: http://server/site/_api/web

User Profile
Client Object Model: ClientContext.Web.Lists
REST EndPoint: http://server/site/_api/Sp.UserProfiles.PeopleManager

Search
Client Object Model: ClientContext.Web.Lists
REST EndPoint: http://server/site/_api/search

Publishing
Client Object Model: ClientContext.Web.Lists
REST EndPoint: http://server/site/_api/publishing

Advantages of using rest api in SharePoint

The advantages of using REST is that you don’t have to add references to any SharePoint 2013 libraries or client assemblies. Instead, you make HTTP requests to the appropriate endpoints to retrieve or update SharePoint entities, such as webs, lists, and list items.

In jsom, SP.js files need to be loaded before the code execution, but in Rest API no such .js files are required.

Another advantages of Rest API in SharePoint is that, Rest API is easier to work and even you can use the browser to see the result and to test them.

SharePoint Rest API Examples:

Below are few examples of SharePoint Rest API.

Retrieve user display name using Rest API in SharePoint Online/2013

Below is the Rest API code to retrieve the user display name using Rest API in SharePoint Online. Here I on a button click I am displaying the display name of the logged in user in SharePoint using Rest API.

<input type="button" id="btnClick" value="Click Here"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script>
$(function(){
$("#btnClick").click(function(){
var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept": "application/json;odata=verbose" };  
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,        
error: onError    
}); 
function onSuccess(data, request) {        
var userinfo = data.d;               
alert ('Display Name: '+ userinfo.Title);  
} 
function onError(error) {        
alert("error");    
}
});
}); 
</script>

Here Title property displays the user display name, similarly, you can use the Email property to retrieve the Email ID of the logged user and you can also use the LoginName property to retrieve the user’s login name in SharePoint Online or SharePoint 2013/2016.

Delete SharePoint 2013/Online list Item using REST API

In this SharePoint Rest API example, we will see how we can delete a SharePoint 2013 list item using REST API. If you want to do SharePoint operation without using server-side code then Rest API is very much useful.

Here I have a button in a web part page and when user clicks on that button, the list item will get deleted.

Copy the below code and paste inside a script editor web part. Here we are retrieving the list item by using the getItemById method.

Here you will see there are 3 items in the list like below and by using REST API we will delete the 2nd item from the list.

delete item from list using rest api in SharePoint 2013
delete item from list using rest api in SharePoint 2013
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('BijayTestList')/getItemById(2)";
$.ajax({
url: requestUri,
type: "DELETE",
headers: {
"accept":"application/json",
"X-RequestDigest":$("#_REQUESTDIGEST").val(),
"IF-MATCH":"*"
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
alert(data+ ‘ Item Deleted’);
}
function onError
(error) {
alert(JSON.stringify(error));
}
});
});
</script>

<input type="button" id="btnClick" value="Click Here"/>

Now you can see the list will have only two items in the SharePoint Online list.

delete item from sharepoint online/2013 list using rest api
delete item from sharepoint online/2013 list using rest api

This is how we can delete list item using Rest API in SharePoint Online or SharePoint 2013/2016.

Delete SharePoint 2013/Online list using REST API

In this SharePoint Rest API example, we will discuss how we can delete a SharePoint 2013/Online list using the REST API. In SharePoint 2013/2016/Online you can use the REST API to do lots of operations.

Here I have a button in a web part page and when a user clicks on that button, the SharePoint list will get deleted.

Copy the below code and paste inside a script editor web part. Here we are retrieving the list by using the getByTitle method.

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

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Columns_List')";
$.ajax({
url: requestUri,
type: "DELETE",
headers: {
"accept":"application/json",
"X-RequestDigest":$("#_REQUESTDIGEST").val(),
"IF-MATCH":"*"
},

success: onSuccess,
error: onError
});

function onSuccess(data) {
alert('List Deleted');
}
function onError
(error) {
alert(error);
}
});
});
</script>

<input type="button" id="btnClick" value="Click Here"/>

This way we can delete a list using Rest API in SharePoint Online or SharePoint 2013.


You may also like following SharePoint Rest API tutorials:

Hope this SharePoint Rest API tutorial helps to learn Rest API in SharePoint and also we saw few SharePoint Rest API examples.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →