Alert This post is over a year old, some of this information may be out of date.

Getting likes and comments from modern pages

If you are using a modern site in SharePoint Online, you might have noticed that there is a new footer / social bar control which contains actions that allow you to like a page, see the number of comments and page views.

Show image The social bar with like and comment capabilities
The social bar with like and comment capabilities

This is a very simple control and might be useful to you when building some social solutions like for example when building your own news page roll-ups or controls. In this article, I show which endpoints are used to retrieve this information from the page.

Getting the page likes

The first party SharePoint controls make use of the following endpoint in order to fetch the page like information: {webUrl}/_api/web/Lists(guid'{listId}')/GetItemById({itemId})/likedBy?$inlineCount=AllPages

You can expect the following data from this endpoint:

Show image Page likes
Page likes

What is important in this call is the $inlineCount parameter, which returns you the number of likes for the provided item.

There is another API which you can use to get the same kind of information and this is the likedByInformation API. You call it as follows: {webUrl}/_api/web/Lists(guid'{listId}')/GetItemById({itemId})/likedByInformation?$expand=likedby.

The returned object looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "@odata.context": "https://....sharepoint.com/sites/APITesting2/_api/$metadata#likedByInformations/$entity",
  "@odata.type": "#Microsoft.SharePoint.Likes.likedByInformation",
  "@odata.id": "https://....sharepoint.com/sites/APITesting2/_api/web/lists('%7Bc3d4c46f-f4ae-454c-bfb7-6608dc01b19f%7D')/GetItemById(23)/likedByInformation",
  "@odata.editLink": "web/lists('%7Bc3d4c46f-f4ae-454c-bfb7-6608dc01b19f%7D')/GetItemById(23)/likedByInformation",
  "isLikedByUser": true,
  "likeCount": 1,
  "[email protected]": "web/lists('%7Bc3d4c46f-f4ae-454c-bfb7-6608dc01b19f%7D')/GetItemById(23)/likedByInformation/likedBy",
  "likedBy": [{
    "@odata.type": "#Microsoft.SharePoint.Likes.userEntity",
    "@odata.id": "https://....sharepoint.com/sites/APITesting2/_api/Microsoft.SharePoint.Likes.userEntity0a0ba3da-55f1-401e-a87e-6c8afa345548",
    "@odata.editLink": "Microsoft.SharePoint.Likes.userEntity0a0ba3da-55f1-401e-a87e-6c8afa345548",
    "creationDate": "2019-09-17T13:27:52.893Z",
    "email": "...",
    "id": 8,
    "loginName": "i:0#.f|membership|...",
    "name": "Elio Struyf"
  }]
}

Getting the page comments

A similar API is used for the page comments. Instead of likedBy, Comments is used: {webUrl}/_api/web/Lists(guid'{listId}')/GetItemById({itemId})/Comments?$expand=replies,likedBy,replies/likedBy&$top=10&$inlineCount=AllPages

Info: Vardhaman Deshpande wrote a good article about this topic last year: Working with the Page Comments REST API in SharePoint Communication sites.

The data this API endpoint returns looks like this:

Show image Page comments
Page comments

Getting the number of likes and comments with one call

If you are only interested in retrieving the likes and comments, there is an easier way than calling the two APIs and extracting the @odata.count property. What you can do is make use of the RenderListDataAsStream in order to fetch this information. The likes count is available in the _likeCount field, and the comments count in the _CommentCount field. Here is a sample:

Show image Using the RenderListDataAsStream API
Using the RenderListDataAsStream API

The body for this request looks as follows:

This is what you could expect in return (check the highlighted lines):

Show image Nr. of likes and comments via the RenderListDataAsStream API
Nr. of likes and comments via the RenderListDataAsStream API

Info: the reason why I used the RenderListDataAsStream API to retrieve these values is because both fields are lookup fields referencing to another site (root site collection in this case). RenderListDataAsStream is the only API which supports it.

Updates

17/09/2019

Added the new likedByInformation API information.

Comments

Back to top