Add comments to SharePoint list items using the REST API

One of the newest features to come to SharePoint Online is the ability to add comments within the activity panel on SharePoint list items. In a previous post, I talked about how we can retrieve these new comments using the SharePoint REST API.

List comments are a great new way for users to collaborate together on list items as users can have threaded conversations with one another.

In the past we would have used a multi-line text column with “append changes” to create threaded conversations on list items. The problem with this is this method creates new versions and those comments are difficult to retrieve programmatically. Please see my post on retrieving values in multi-line text fields more efficiently.

Add a comment using the REST API

Recently, I’ve been seeing more posts on support forums and in social asking how these comments can be accessed and created programmatically. Well, like most things in the modern interface, they are implemented using client-side code, which must mean there is a REST API backing the functionality.

You will need to execute a POST request to the following endpoint

Endpoint https://yourtenant.sharepoint.com/sites/ContosoGroup/_api/web/lists/GetByTitle(“<YourListName>&#8221;)/items(<ItemId>)/Comments()

Parameters – The parameter you will pass through the body is called “text”. Construct your JSON object as {“text”:”your sample comment”}

fetch("https://yourtenant.sharepoint.com/sites/ContosoGroup/_api/web/lists/GetByTitle('Test')/items(<ItemId>)/Comments()", {
"headers": {
"accept": "application/json;odata=verbose",
"content-type": "application/json;charset=UTF-8",
"x-requestdigest": "<yourRequestDigest""
},
"body": "{\"text\":\"Add a new comment\"}",
"method": "POST",
"mode": "cors",
"credentials": "include"
});

Using the above POST call, you can submit a new comment to the list item as the current user (there is no way to add list item comments on-behalf of another user). When you execute the request and update your x-requestdigest, and the appropriate values for the URI, your comment will be successfully added to the list item (see below).

I hope you find this post helpful and if you have any questions, please feel free to reach out!

4 thoughts on “Add comments to SharePoint list items using the REST API

  1. Molly Yanus January 18, 2021 / 10:51 am

    Is this a workaround to log when a cell in a metadata column changes from one dropdown option to another? – Or is there already a better way to track that? –

    Example – cell in a “Status” metadata/dropdown column changes from “Not Started” to “In Progress” – logs a comment by the user that changed the cell status…… I know its in the activity log, but I haven’t been able to alienate the data correctly to track only status changes…..

    Ignore if I’m totally off base! We know I’m only technical enough to be dangerous!

    Like

    • Beau Cameron January 18, 2021 / 12:39 pm

      Yea it’s different… but that’s a pretty cool idea. That may be something we could pull from Microsoft Graph.

      Like

  2. Derek March 13, 2021 / 6:07 pm

    I spent hours trying to figure out why this didnt work. As it happens the sharepoint list name should be in single quotes, not double. Like this: GetByTitle(‘listname’), not GetByTitle(“listname”)

    The issue I now have is that I get an error saying the JSON is invalid but that is too simple to be wrong unless there’s been a syntax change?

    Like

    • Beau Cameron March 16, 2021 / 3:25 pm

      Hi! Apologies on that error, I clean up the calls and I messed up on that.

      Are you able your call and your JSON with me? I’ll take a look!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s