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.
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:
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:
|
|
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:
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:
The body for this request looks as follows:
This is what you could expect in return (check the highlighted lines):
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.