Post comment to a modern site page using REST api

matt howell 331 Reputation points
2021-06-14T17:25:55.42+00:00

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 Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,615 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 30,186 Reputation points Microsoft Vendor
    2021-06-15T07:34:00.453+00:00

    Hi @matt howell ,
    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.


  2. matt howell 331 Reputation points
    2021-06-15T13:36:28.247+00:00

    @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?