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 Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,551 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,598 questions
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,662 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,431 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 30,991 Reputation points Microsoft Vendor
    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.