Wednesday 26 July 2017

Working with the Page Comments REST API in SharePoint Communication sites

If you have been playing around with SharePoint Communication sites, you might have noticed there is commenting functionality available now on site pages. Digging deeper on how this functionality is implemented gives us some interesting findings!

There is a Comments REST API available which is used to get and post comments for a particular page. This endpoint is an addition to the SharePoint REST API, which means you will already be familiar with using it.

I checked with Vesa Juvonen from Microsoft and he has confirmed this is indeed a public API, which means it can be used in third party solutions and is not something internal used only by Microsoft.


There are a few interesting things about how the commenting solution is implemented:
  1. Comments are not stored in the page list item (which makes sense in terms of scalability). They appear to be stored in a separate data store.
  2. Comments are stored with references to list guids and item ids. This means that if you move or copy a page, comments for that page will be lost.
  3. Only a single level of replies is allowed. Which in my opinion is a good thing as this will prevent long winding conversations and force users to keep their comments brief.

Now lets have a look at the SharePoint Comments REST API:

1) Get comments for a page:

/_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')/GetItemById(1)/Comments

This will bring back all the top level comments for a page which has the id 1 and lives in a site pages library with guid "1aaec881-7f5b-4f82-b1f7-9e02cc116098". 

2) You can also use the list title to get the list and the comments:

/_api/web/lists/GetByTitle('Site Pages')/GetItemById(1)/Comments

3) Get replies for each comment:

/_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')/GetItemById(1)/Comments?$expand=replies

4) Get replies for a specific comment:

/_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')/GetItemById(1)/Comments(2)/replies

Where 2 is the id of the comment. 

5) Post a new comment on a page by making a POST request to:

/_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')/GetItemById(1)/Comments

6) To Delete a comment or a reply, Make a DELETE request to:

/_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')/GetItemById(1)/Comments(2)

Since each comment or reply gets a unique id, the method is same for deleting both.

Here is some sample code I put together to use the Comments REST API in an SPFx webpart:


Get comments for a page:



Post a comment on a page:


Code for this web part available on GitHub: https://github.com/vman/spfx-sitepage-comments/

Quick note about running the webpart on the SharePoint Workbench: If the page on which you want to read or post comments lives in a communication site with url:

https://tenant.sharepoint.com/sites/comms/SitePages/ThisIsMyPage.aspx

Then make sure you are running the workbench from the same site:

https://tenant.sharepoint.com/sites/comms/_layouts/15/workbench.aspx

Hope you found this post interesting!

29 comments:

  1. Any idea how to prevent Comments from showing up on the page. We don't want to see comments on the home page of a site

    ReplyDelete
  2. Hi,

    You can disable the comments on a page when you edit the page.

    Thanks

    ReplyDelete
  3. Hi

    Is this the correct GUID for the list ? - /_api/web/lists('1aaec881-7f5b-4f82-b1f7-9e02cc116098')

    I have tried running your SPFx webpart which comes back and says 404 object not found.

    I am trying to find the comments list for a Site-Page from a Communications Site.

    Regards

    Nigel

    ReplyDelete
  4. Hi Nigel,

    The guid for the Site Pages library on your site will be different. Alternatively, you can also use the title to get the Site Pages library.

    Thanks

    ReplyDelete
  5. Hi

    Thanks for the reply, I think you mean for the Comments Library not the site pages library. Can I use a partial match in REST CALLs. eg can I look for lists that start with "Comments" ? If not how can I find the title of the Hidden Comments list ?

    Thanks

    Nigel

    ReplyDelete
  6. Hi Nigel,

    There is no library in the site where the Comments are stored. As mentioned in the post, they are stored in a separate data store which can only be accessed with the REST API. The way to access the comments with the REST api is to do a /Comments after the path to the page.

    Thanks

    ReplyDelete
  7. Hi

    Thanks for this. What I was hoping was that I could attach an event to the comments list (which of course now does not exist) so that I could set up some sort of moderation when a comment is posted. Is there no way I could detect when a comment is posted then ?

    Thanks

    Nigel

    ReplyDelete
  8. Hi

    Why don't we have any feature to edit the comments, as we used to have in SharePoint blog sites ?

    Also is there any way to create alerts to the comments?

    Thanks

    Akshay

    ReplyDelete
  9. Is there a way to moderate comments in SharePoint Online? We are currently in an on-prem version of SharePoint (wanting to move to SPO) and our current tenant allows us to moderate comments before they are posted.

    ReplyDelete
  10. hi ,
    any information about the retention policy for the comments , how long they will be available ? is there any time limit which defines when the Much Older comments will not appear any more?
    Any response will be appreciated.

    ReplyDelete
  11. Hi !
    Can you help me, I'm trying to call the function to get the number of comments from the modern page, but I can not correctly display the result in the html code.
    I put code to site: https://sharepoint.stackexchange.com/questions/244795/sharepoint-framework-call-async-function?noredirect=1#comment261037_244795
    Thanks!

    ReplyDelete
  12. Is this REST end point available for Classic Sharepoint pages

    ReplyDelete
  13. Is there a way to change the authorship of the comment.
    I want to post the comment using a service account and them update the "Author" field for the comment to another user.
    Is it possible? If yes how?

    ReplyDelete
  14. Hi @vardhaman, I tried this rest api today in a communication site and it returned 403 status code (Access denied). What could I be overlooking at this point?

    Thanks,
    Rabia Williams

    ReplyDelete
  15. Also is there a SharePoint webhook available when new comments/replies are posted on a modern page?

    ReplyDelete
  16. Hi, can i use these api's for the sharepoint cutoms list or its only for site pages library

    ReplyDelete
  17. Is there any possibility/way to display comments for last 2 month instead of all comments?

    ReplyDelete
  18. Does anybody know, how to post a „like“ in Modern Sites?Is there a REST call available like for posting a comment? Help would be appreciated much.

    ReplyDelete
  19. Hi Vardhaman,
    above you explain how to post a comment on a page in a SharePoint Communication site. Is it possible to send a like to a page? Is there a REST API for Likes? If this is the case how does this work?
    Thanks in advance for any help by you or whoever has an answer!
    Emmo

    ReplyDelete
  20. Is there a way to update the author of the comment? I have to do a system update to preserve modified, created, author details. I need to create a comment on behalf of someone else. Is there a way?

    ReplyDelete
  21. How to find the guid of SharePoint news pages?

    ReplyDelete
  22. Will this work for SharePoint online

    ReplyDelete
  23. In the post method what should be there in the body

    ReplyDelete
  24. Is there any way to update the author of the comment and datetime on which the comment is added?

    ReplyDelete
  25. In a SharePoint page we also have the option to turn the comments off.
    Is there a way to know whether the comments for a particular page is turned off or not using the REST Api?

    ReplyDelete
  26. If you got here wondering about updating the author and date, as a number of people asked, the answer is NO you can't do that. You can only post the text of the comment.

    ReplyDelete
  27. Is there any way to add list item comments on-behalf of another user?

    ReplyDelete
  28. We are trying to retrieve all comments (with replies) and no paging. However, when we execute GetItemById(1)/Comments?$expand=replies it is not showing any of the replies (isReply is always false). Any insight as to why?

    ReplyDelete

Comments moderation is turned ON. Your comment might not appear immediately after posting.