I had a requirement to get all the social comments from a news board web part for a specific page. Though this can be achieved using "Server Side Object Model," it is restricted for some of the companies to run their "SSOM" code on production environment. Thus, we came up with an idea of creating PowerShell script using CSOM to retrieve social comments using "Social Data Web Service".
Here you will be seeing how to get all the social comments in SharePoint 2013 using the PowerShell + CSOM for a specific page using "Social Data WebService" (SocialDataService.asmx)
The below code retrieves all the comments for the specified page URL and exports the resulting data to Excel.
Code Usage
  1. ##Create an empty System.Array object
  2. $PageNoteBoardComments = @()
  3. ## Web Service Reference - http://Site/_vti_bin/SocialDataService.asmx
  4. $uri="http://<sitepath>/_vti_bin/SocialDataService.asmx?wsdl"
  5. ##PageURL to be specified as input parameter of which contains social comments.
  6. $url="http://<sitepath>/Pages/TopNews.aspx"
  7. ## if you are executing this code in a Virtual Machine it's default credentials will be used to connect to proxy.
  8. $socialDataServiceWebServiceReference = New-WebServiceProxy -Uri $uri -UseDefaultCredential
  9. ##Get total number of comments for a specified URL
  10. [int]$CountCommentsOnUrl=$socialDataServiceWebServiceReference.CountCommentsOnUrl($url)
  11. Write-Host -ForegroundColor Green "Number of comments for the specified URL : " $CountCommentsOnUrl
  12. ##Get Comments for the URL Specified
  13. $CommentsOnUrl = $socialDataServiceWebServiceReference.GetCommentsOnUrl($url,$null,$null,$null)
  14. ##Iterate on the comments collection and store the value in PageNoteBoardComments array object.
  15. foreach($item in $CommentsOnUrl)
  16. {
  17. $PageNoteBoardComments += New-Object
  18. PsObject -property @{
  19. 'Comments' = $item.Comment
  20. 'Last Modified Time' = $item.LastModifiedTime
  21. 'URL' = [string]$item.Url
  22. 'Owner' = [string]$item.Owner
  23. 'Page Title' = [string]$item.Title
  24. }
  25. }
  26. ##Finally, use Export-Csv to export the data to a csv file
  27. $PageNoteBoardComments | Export-Csv -NoTypeInformation -Path "D:\Madhu\Powershell\NoteBoardComments.csv"
Scenario 2
To iterate all the page URL's from excel and to retrieve all the social comments from social data web service based on page URL.

Note
All the Page URL's are stored under "PageUrl" column in excel sheet.
  1. #Path to the CSV output path file.
  2. $CSVOutputfilepath = "D:\Madhu\GetSocialComments.csv"
  3. #Path to the log file.
  4. $outputfilepath = "D:\Madhu\GetMySocialCommentsLogs.txt"
  5. #Excel File path.
  6. $FilePath = "D:\Madhu\pagelist.xls"
  7. #Create an Object Excel.Application using Com interface
  8. $objExcel = New-Object -ComObject Excel.Application
  9. # Disable the 'visible' property so the document won't open in excel
  10. $objExcel.Visible = $False
  11. # Open the Excel file and save it in $WorkBook
  12. $WorkBook = $objExcel.Workbooks.Open($FilePath)
  13. # Load First sheet
  14. $sheet = $WorkBook.Worksheets.Item(1)
  15. #Find PageUrl and get the column position
  16. $pageurlcolumns = $sheet.UsedRange.Columns.Find('PageUrl').Column
  17. #Get all the Rows within Excel
  18. $rows = $sheet.UsedRange.Rows.Count
  19. ## Get number of user in excel
  20. $rowCount = $rows - 1
  21. "-"*80 | Write-Host
  22. Write-Host -ForegroundColor Green "Total number of the pages initiated : " $rowCount
  23. Write-Host -ForegroundColor Green "Started at : $((Get-Date).ToString())"
  24. "-"*80 | Write-Host
  25. "-"*80 | Out-File -Encoding Ascii -append $outputfilepath
  26. "Total number of the pages initiated : " + $rowCount | Out-File -Encoding Ascii -append $outputfilepath
  27. "Started at :" + $((Get-Date).ToString()) | Out-File -Encoding Ascii -append $outputfilepath
  28. "-"*80 | Out-File -Encoding Ascii -append $outputfilepath
  29. ## Get Page Number Count
  30. $count = 1;
  31. # loop for each row of the excel file
  32. for ($i=2; $i -le $rows; $i++)
  33. {
  34. try{
  35. #Get PageUrl column values
  36. $filePath = $sheet.Cells.Item($i, $pageurlcolumns).text
  37. #Log the result to log file
  38. Write-Host -ForegroundColor Green "$count : " $filePath
  39. "$count : " + $filePath | Out-File -Encoding Ascii -append $outputfilepath
  40. ## $url is a string that contains the URL from where we need to count the social comments
  41. ## list contains the pages from pages library and hence we got a substring by spliting the url.
  42. $url=$filePath.Substring(0,$filePath.IndexOf('/Pages'))
  43. ## Get path of socail comments webservice file
  44. $uri = $url + "/_vti_bin/SocialDataService.asmx"
  45. ## Web Service Reference - http://Site/_vti_bin/SocialDataService.asmx
  46. $socialDataServiceWebServiceReference = New-WebServiceProxy -Uri $uri -UseDefaultCredential
  47. ##Retrieve number of comments for a specific page.
  48. [int]$CountCommentsOnUrl=$socialDataServiceWebServiceReference.CountCommentsOnUrl($filePath)
  49. Write-Host -ForegroundColor yellow "Number of comments for the specified URL : " $CountCommentsOnUrl
  50. "Number of comments for the specified URL : " + $CountCommentsOnUrl | Out-File -Encoding Ascii -append $outputfilepath
  51. ##Retrieve comments for a specific page.
  52. $CommentsOnUrl = $socialDataServiceWebServiceReference.GetCommentsOnUrl($filePath,$null,$null,$null)
  53. foreach($item in $CommentsOnUrl)
  54. {
  55. $PageNoteBoardComments += New-Object PsObject -property @{
  56. 'Comments' = $item.Comment
  57. 'URL' = [string]$item.Url
  58. 'Last Modified Time' = $item.LastModifiedTime
  59. 'Owner' = [string]$item.Owner
  60. }
  61. ##Add comments for a specific page.
  62. #$WriteCommentsOnUrl = $socialDataServiceWebServiceReference.AddComment($url,$item.Comment,0,"TestMultiline")
  63. }
  64. $count++
  65. }
  66. catch{
  67. Write-Host -ForegroundColor Green "$accountID : " $_.Exception.Message
  68. "$accountID : " + $_.Exception.Message | Out-File -Encoding Ascii -append $outputfilepath
  69. }
  70. }
  71. #Finally, use Export-Csv to export the data to a csv file
  72. $PageNoteBoardComments | Export-Csv -NoTypeInformation -Path $$CSVOutputfilepath
  73. "-"*80 | Write-Host
  74. "-"*80 | Out-File -Encoding Ascii -append $outputfilepath
  75. Write-Host -ForegroundColor Green "Completed at : $((Get-Date).ToString())"
  76. "Completed at : " + $((Get-Date).ToString()) | Out-File -Encoding Ascii -append $outputfilepath
  77. Write-Host -ForegroundColor Green "Total Comments Found : " $PageNoteBoardComments.Count
  78. "Total Comments Found : " + $PageNoteBoardComments.Count | Out-File -Encoding Ascii -append $outputfilepath
  79. "-"*80 | Write-Host
  80. "-"*80 | Out-File -Encoding Ascii -append $outputfilepath
  81. #Close the workbook object
  82. $WorkBook.close()
  83. #close the excel object
  84. $objexcel.quit()
Please feel free to share your comments.
I hope this helps!!!!!