how to get total number of counts in Sharepoint using search api

Mahima Kankriya 1 Reputation point
2023-02-07T11:46:51.0633333+00:00

Hi,

How can I get "Likes & Comments" count in Sharepoint/Sharepoint site using Search Api.

Thanks in advance!!

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,446 Reputation points
    2023-02-07T12:46:43.3466667+00:00

    Hi Mahima Kankriya,

    Hope you are doing well.

    As per my research, currently there is no Graph API endpoint to get the comments and likes of a site page in the document.
    You may try to use SharePoint Rest API to get the comments.

    To get Comments details:

    /_api/lists/getbytitle('Site Pages')/items(pageID)/comments

    or

    /_api/web/Lists(guid'{listId}')/GetItemById({itemId})/Comments?$expand=replies,likedBy,replies/likedBy&$top=10&$inlineCount=AllPages

    To get Like details:

    /_api/web/Lists(guid'{listId}')/GetItemById({itemId})/likedBy?$inlineCount=AllPages

    Liked by Information:

    /_api/web/Lists(guid'{listId}')/GetItemById({itemId})/likedByInformation?$expand=likedby

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.


  2. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-02-08T06:52:01.83+00:00

    Hi @Mahima Kankriya

    Per my research, there is no such function to retrieve item comments and likes. Search API is unable to expand the properties of comments and likes. If you want to retrieve comments, I will recommend you to use powershell to get them. Please make a reference

    #Connect to SPO site
    Connect-PnPOnline -Url https://<TENANT-NAME>.sharepoint.com/sites/<YOUR-SITE>
    
    #Store pages into a variable
    $sitePagesGallery = Get-PnPList -Identity "Site Pages"
    
    #Get the pages details 
    $logFile = "C:\users\$env:USERNAME\Desktop\LikesAndComments.csv"
    $results = @()
    
    foreach ($item in $sitePagesGallery) {
        $allPages = Get-PnPListItem -List $sitePagesGallery -Fields "FileLeafRef", "Title", "ID", "FileRef", "_CommentCount", "_LikeCount"
        
        #Choose the properties to export
        foreach ($page in $allPages) {
            $results += New-Object -TypeName psobject -Property @{
                Title         = $page["Title"]
                ID            = $page.ID
                FullPath      = $page["FileRef"]
                NumOfComments = $page["_CommentCount"]
                NumOfLikes    = $page["_LikeCount"]        
            }
        }
    }
    $results | Export-Csv -Path $logFile -NoTypeInformation
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.