Working with Likes & Replies in SharePoint REST API

Posted by: Bergen Rackley on May 27,2020

Recently, while working on a webpart that mimicked functionality provided for SharePoint News Articles, I came across several issues where solutions were either barely documented or non-existent. After several hours of Googling, troubleshooting, reverse engineering, and guessing I found discovered various obscure solutions to all my problems.

Problem #1: Replying to pre-existing comments on News Articles

Problem #2: Liking an article

Problem #3: Liking comments/replies

 

Problem #1: Replying to pre-existing comments on News Articles

Creating a comment on a news article is simple:

MicrosoftTeams-image (1)-1

After structuring the ‘body’ json in ‘spOpts,’ all that needs to be done is submit a POST request to:

/_api/web/Lists/GetByTitle('Site Pages')/GetItemById(${id})/Comments

and the comment will be posted. “id” is the ID of the article the comment is posted on. Replying to a comment is just as simple, yet from what I could tell this isn’t documented anywhere else. The ‘body’ is identical, and all that changes is the post request, and the code now looks like:

MicrosoftTeams-image (2)-1

Adding ‘(<commentID>)/replies’ to the end of the query string will post the comment as a reply to whatever ID is input into <commentID> (All comments on a page have unique IDs like items in libraries do. They reset across pages so specifying the article is still required, so for this POST request to be dynamic you’ll want to make BOTH the article ID and the Comment ID variables).

 

Problem #2: Liking an article

As this webpart needed to be able to do anything you could do from the page view of the article, I needed to figure out how to like a comment with REST API. Documentation for doing this may exist somewhere, but after a little Googling I couldn’t come up with anything, but was able to figure it out myself. Using Fiddler, I liked the article from the webpage and caught a post request to this url:

/_api/web/Lists/GetByTitle('Site Pages')/GetItemById(${id})/unlike

I created a webpart for testing POST requests (lots of trial and error means needing a workbench to mess around with) and ran a POST request to this url, and after a little troubleshooting I came up with this:

MicrosoftTeams-image (3)-1

This POST request is even simpler than posting comments, as it doesn’t require any parameters. Running this POST request doesn’t return any object, but if you check the Network panel in your debug tools you’ll see a ‘like’ appear in the network activity, and going to the news article will show that you have now liked it. Similarly, if you wish to unlike the post, run this:

MicrosoftTeams-image (4)

Once again, no object will be returned but upon reloading the page the article will be unliked.

 

Problem #3: Liking comments/replies

This was less of a ‘problem’ and more of another seemingly undocumented solution, as I tried changing the post request a little bit and it worked first try. If you need to be able to like a comment on a news article, run a POST request as follows:

MicrosoftTeams-image (2)-1

Similarly, to replying to comments, liking a comment only requires adding ‘/Comments(<commentID>)/like’, commentID once again being the ID of the comment you’re targeting. To unlike a comment, use:

MicrosoftTeams-image (2)-1

Just as what happens when liking an article, liking/unliking a comment will not return anything but will show up in your network activity.

To sum this all up, I can only imagine this documentation is sparse/nonexistent because it’s not going to be used very often. There are only so many use cases for liking comments/articles and viewing comments. BUT if you need these solutions I hope this article was brief enough to keep your attention, detailed enough to explain what to do, and most importantly, saved you the several hours I spent figuring it all out.

Topics: SharePoint REST API

Subscribe Today!

Recent Posts

Categories