Azure / Office 365 / SharePoint Development / Search

Getting likes and comments from modern pages

March 26, 2018

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.

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:

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.

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:

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:

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):

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.

Comments