The Modern SharePoint site pages have a lot of hidden features. One of them is the “Like this page” social feature. This feature enables the option to like the modern site page. In this post, we’ll see how to get the total likes and see who has liked the page of the modern SharePoint site page.
The below SharePoint Rest API is used to retrieve the liked Information of the page item.
Format of the Request URL:
https://domain.sharepoint.com/sites/name/_api/web/lists/getbyTitle('<Library Name>')/GetItemById(<Page List Item ID>)/likedByInformation?$expand=likedby&$select=likedby
To test this, open the browser console and paste the below code and make the changes to the site URL, library name and Item ID:
-
-
- function getRequest(url) {
- var request = new XMLHttpRequest();
- return new Promise(function(resolve, reject) {
- request.onreadystatechange = function() {
- if (request.readyState !== 4) return;
- if (request.status >= 200 && request.status < 300) {
- resolve(request);
- } else {
- reject({
- status: request.status,
- statusText: request.statusText
- });
- }
- };
-
- request.open('GET', url, true);
- request.setRequestHeader("Content-Type", "application/json;charset=utf-8");
- request.setRequestHeader("ACCEPT", "application/json; odata.metadata=minimal");
- request.setRequestHeader("ODATA-VERSION", "4.0");
- request.send();
-
- });
- }
-
-
-
- getRequest("https://domain.sharepoint.com/sites/name/_api/web/lists/getbyTitle('Site Pages')/GetItemById(1)/likedByInformation?$expand=likedby&$select=likedby").then(function(output) {
- var result = JSON.parse(output.response);
- var strMessage = "Total Likes: " + result.likeCount + "\r\n";
- for (var i = 0; i < result.likedBy.length; i++) {
- strMessage += result.likedBy[i].name + "\r\n";
- }
- alert(strMessage);
- });