question

matthowell-6829 avatar image
0 Votes"
matthowell-6829 asked IvanYakushchenko-0997 commented

Post comment to a modern site page using REST api

Using this endpoint:

https://xxx.sharepoint.com/sites/xxx/_api/web/Lists/GetByTitle('SitePages')/GetItemById(305)/Comment...

And this listitementitytypefullname:

SP.Data.SitePagesItem
Response:
The property '__metadata' does not exist on type 'Microsoft.SharePoint.Comments.comment'. Make sure to only use property names that are defined by the type.
Where is it getting the "type" from? Shouldn't it be using the listitementitytypefullname? And if that's the wrong type, reflect that in the error? And if wrong, what should the listitementitytypefullname: be for site page comments?

sharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matthowell-6829 avatar image
0 Votes"
matthowell-6829 answered

I switched to the app I'd registered in Azure AD and that allows user context. More difficult to set up than an app only app in SP, but it works.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RaytheonXie-MSFT avatar image
0 Votes"
RaytheonXie-MSFT answered IvanYakushchenko-0997 commented

Hi @matthowell-6829 ,
When I tested the endpoint ,the same error occurred. Then I try following url, and it works. You also can try it.
https://xxx.sharepoint.com/sites/xxx/_api/web/Lists/GetByTitle('SitePages')/GetItemById(305)/Comments

Here is my code for sample:

         $.ajax
             ({
                 url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('Site%20Pages')/GetItemById(34)/Comments",
                 type: "POST",
                 headers:
             {
                 "Accept": "application/json;odata=verbose",
                 "Content-Type": "application/json;odata=verbose",
                 "X-RequestDigest": $("#__REQUESTDIGEST").val()
             },
                 data: JSON.stringify
             ({
                 __metadata:
                 {
                     type: "Microsoft.SharePoint.Comments.comment"
                 },
                 text: "new comment by restapi-01"
             }),
                 success: function (data, status, xhr) {
                        
                 },
                 error: function (xhr, status, error) {
                        
                 }
             }); 


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is there a way to post comment on behalf of another user? I'd like to migrate comments from one site to another by using these REST API functions.
When I use access token for REST API calls, then comments are posted on behalf of logged in user. Is there a way to include user data in POST request body?

0 Votes 0 ·
matthowell-6829 avatar image
0 Votes"
matthowell-6829 answered RaytheonXie-MSFT commented

@RaytheonXie-MSFT Yep, I had tried Microsoft.SharePoint.Comments.comment as the listitementitytype originally and it failed so I wrongly thought that was the problem. Now I tried again and it works. Any idea how to show the user as the commenter instead of the app name "SP App"? If so, how?

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @matthowell-6829 ,
Did you use the app only for authentication? If so the commenter will show the app name "SP App". To show the user as commenter, you need to use the user for authentication

0 Votes 0 ·

Most likely yes. We're testing api endpoints for inclusion in a native react app. How would I use user auth instead of app auth? @RaytheonXie-MSFT

0 Votes 0 ·