Exposing SharePoint Services as an APIs
In this article, we will learn about the basic understanding of REST API in SharePoint and will understand how to exposing (provide) SharePoint API by developing a middleware application that works between SharePoint and any third party, in SharePoint online thru the CRUD operations in the SharePoint REST API. Throughout this article we will focus on the below:
- What is the SharePoint REST API?
- What is the use of the REST API in SharePoint?
- REST API HTTP methods — HTTP operations in SharePoint using REST API
- What is GET, POST, PUT and DELETE requests in HTTP?
- What is the difference between PUT POST and PATCH with examples?
- SharePoint Online RESTful API
- SharePoint REST API operations
- Proof of Concept (POC) Source Code in my GitHub Account
You can clone my solution and try to expose your SharePoint services:
What is the SharePoint REST API?
One of the most popular types of API is REST or, as they’re sometimes known, RESTful APIs. The REST API is a data-centric web service based on the Open Data Protocol or OData. The way these web services work, use each resource in the system is addressed by a specific URL that you pass on to the server. SharePoint 2013 has a REST API that exposes plenty of information about sites, users, lists and document libraries, etc.
The REST API (Representational State Transfer) service in SharePoint 2013 is another client object model technique like CSOM, JSOM, etc. which we used to do earlier.
Example to Get data by using ajax or curl:
$. ajax ({
url: URL, //This is the ENDPOINT URL
method: "GET",
headers: {"Accept": "application/JSON; odata=verbose"},
success: function (data) {
console.log (data.d. email) //GET operation results will be here !!!
}
});******************************************************************curl --location --request GET 'http://<site url>/_api/web/lists/GetByTitle('\''NameOfList'\'')'
What is the use of the REST API in SharePoint?
One of the key advantages of REST APIs is that they provide a great deal of flexibility. Data is not tied to resources or methods, so REST can handle multiple types of calls, return different data formats. When we want the universal presence with minimum efforts, given the fact that REST APIs are exposed as an HTTP Service, which is virtually present on almost all the platforms. Using REST API remotely we can interact with SharePoint 2013/2016/2019/Online sites and can perform create, read the update, and delete(CRUD) operations.
REST API HTTP methods - HTTP operations in SharePoint using the REST API:
We can perform the below HTTP operation in SharePoint using REST API:
GET, POST, PUT, MERGE, and PATCH these methods are responsible for CREATE, READ, UPDATE, and DELETE (REST CRUD) operation in SharePoint.
In simple understanding we can define as below:
GET: GET method is used to retrieve or get the information from the given server using a given URI i.e Uniform Resource Identifier. When we use GET request only the data gets retrieved and in REST CRUD, it performs the READ operation.
POST: POST is used for sending the data to the server i.e uploading a file or transferring some data or adding a new row to the back end table to any kind of web form. POST is nothing but inserting new items in the backend server — in REST CRUD, it performs the CREATE operation.
PUT: PUT is used for replacing the new data with existing data at the target resource.
What is the difference between POST, PUT, and PATCH methods of an HTTP protocol?
POST
An HTTP.POST method always creates a new resource on the server. It’s a non-idempotent request, i.e. if a user hits the same requests 2 times it would create another new resource if there is no constraint.
http post method is like a INSERT query in SQL which always creates a new record in database.
Example: We can use the POST method in scenarios like to save new users, sales orders, purchase orders, etc. where the backend server decides the resource id for the new resource.
PUT
In HTTP.PUT method, the resource is first identified by the URL and if it exists, then it is updated, otherwise, a new resource is created. When the target resource exists, it overwrites that resource with a completely new body. That is HTTP.PUT method is used to CREATE or UPDATE a resource.
http put method is like a MERGE query in SQL which inserts or updates a record depending upon whether the given record exists.
A PUT request is idempotent i.e. hitting the same requests twice would update the existing record (no new record will be created). In the PUT method the resource id is decided by the client and provided in the request URL.
Example: Use the PUT method to update existing users, Sales Oder, and Purchase Order, etc.
PATCH
An HTTP.PATCH method is used for partial modifications to a resource i.e. delta updates.
http patch method is like a UPDATE query in SQL which sets or updates selected columns only and not the whole row.
Example: We could use the PATCH method to update any status column like document life cycle status, approval status, migration status, and update certain columns in a SharePoint list or library, etc.
REST API endpoint URL SharePoint:
- /_api/Web/Lists
Retrieving all lists on a site and adding new lists to the site.
- /_api/Web/Lists/GetByTitle(‘listname’)
Getting list details by the list title and updating it as well. If anyone changes the list title, the existing code will break.
- /_api/Web/Lists(guid’guid id of your list’)
Same as above but changing the list title will not affect the code. This is an advantage over the other method.
- /_api/Web/Lists/GetByTitle(‘ listname ‘)/Fields
Retrieving all fields/columns associated with a list and add new fields to the list.
- /_api/Web/Lists/GetByTitle(‘listname’)/Fields/GetByTitle(‘fieldname’)
Getting details of a field, will be used in modifying and deleting fields.
- /_api/Web/Lists/GetByTitle(‘listname’)/Items
Retrieving all items in a list and adding new items to the list.
- /_api/web/lists/GetByTitle(‘listname’)/GetItemById(itemId)
This endpoint can be used to get, update and delete a single item — it works on a particular item based on its ID.
SharePoint Online RESTful API:
Below is the list of other useful SharePoint online RESTful API that are frequently used in our daily SharePointing life:
Site Information API:
Endpoint URL: http://server/site/_api/Web/SiteUserInfoList
Users Information API:
- To get current user Information by using this API:
Endpoint URL: http://server/site/_api/Web/currentUser - To get all site users information by using this API:
Endpoint URL: http://server/site/_api/Web/siteusers - To get particular group site users information by using this API:
Endpoint URL: http://server/site/_api/Web/sitegroups(GroupId)/users - To get particular group site users information by using this API:
Endpoint URL: http://server/site/_api/Web/sitegroups(GroupId)/users/getbyid(UserId)
Group Information API:
- To get all group information:
Endpoint URL: http://server/site/_api/Web/sitegroups - To get group information by using groupID:
Endpoint URL: http://server/site/_api/Web/sitegroups/GetById(GroupId) - To get a user group:
Endpoint URL: http://server/site/_api/Web/ GetUserById(“+UserID+”)/Groups Endpoint URL: http://server/site/_api/Web/ currentuser/? $expand=groups’
LISTS Information API:
- To get all properties from the list:
Endpoint URL: http://server/site/_api/web/lists - To get a particular property of the list:
Endpoint URL: http://server/site/_api/web/lists/getbytitle(‘Title’) - To get the selected fields list:
Endpoint url: http://server/site/_api/web/lists/getbytitle(‘Title’)/Items?select=ID,Title - To get the selected fields list by using filter:
Endpoint url: http://server/site/_api/web/lists/getbytitle(‘Title’)/Items?select=ID,Title&$filter=((ID eq 1) and (Title ne ‘yyyy’)) - To get the selected fields list by using Expand:
Endpoint url: http://server/site/_api/web/lists/getbytitle(‘Title’)/Items?select=*,list2/Title,list3/Title&$filter=((ID eq 1) and (Title ne ‘yyyy’))&$expand=list2, list3