2

I have a requirement to fetch all the comments from all the site pages in a SharePoint Online site collection date wise. Is there a way to achieve it using Powershell?

I will be creating a timer job and running this query to send a notification to business users.

1 Answer 1

1

You can use the REST API to call the /Comments endpoint:

/_api/web/lists/GetByTitle('Site Pages')/GetItemById(x)/Comments

Here is the PowerShell code:

$SiteURL = "https://yourtenant.sharepoint.com/sites/TargetSiteCollection"

Connect-PnPOnline $SiteURL -UseWebLogin

$ListName= "Site Pages"

$allSitePages = Get-PnPListItem -List $ListName

foreach ($sitePage in $allSitePages)
{
    Write-host ------------------------------
    $pageID = $sitePage["ID"]
    Write-host 'Site Page' $pageID ':' $sitePage["Title"]
    $RestMethodURL = $SiteURL+"/_api/web/lists/GetByTitle('$ListName')/GetItemById('$pageID')/Comments"
    $ListOfComments = Invoke-PnPSPRestMethod -Url $RestMethodURL
    if ($ListOfComments.value)
    {
        # Comments exsist!
        Write-host $ListOfComments.value.Count comments found:
        $ListOfComments.value
    } else {
        Write-host "No comments where found on this page."
    }
         
    Write-host ------------------------------ `n`n`n`n`n
}

Here is the PowerShell script output compared to the comments on the SharePoint page: PowerShell Output vs Comments on SharePint Page

5
  • @Sivanrv, is this what you are looking for? Thanks! Jun 2, 2022 at 13:21
  • Hi...thanks for your answer, but I'm facing issue in fetching all the comments using web api. Getting partial commments only
    – Sivanrv
    Jun 8, 2022 at 10:44
  • @Sivanrv, are you using the Invoke-PnPSPRestMethod and only getting partial comments? Jun 8, 2022 at 11:12
  • even in web response I'm not getting if comments are more than 50. Ex: Comment count showing in page is 50 but in response I'm getting only around 35. Is replies not counted?, if so, can we include that in script by individual calls?
    – Sivanrv
    Jun 9, 2022 at 13:41
  • @Sivanrv, yes you're right. Unfortunately, replies to comments are not counted :( Jun 9, 2022 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.