In my previous article, I explained how to iterate the list item and send a consolidated email to respective recipients using SharePoint designer workflow. Now, I am going to do the same process using PnP PowerShell module.

In this experiment, I am going to read the list of manager names from the Employee Database List and will send a consolidated employees information to the respective managers. This script may help you to automate any business process in your organization.
So, for test purposes, I have created one custom list called “Employee Database”. Then, I created some columns like Employee ID, Employee Name, Department, and finally, Manager Name. I have created all columns as single-line-of-text for demo purposes and added a few rows. So, while you are creating the list, you can use the respective data type like people picker for Employee name and Manager Name, etc.
Send consolidated list item to the recipient using PnP PowerShell
Let’s get started with the script. Here, I used the below structure to construct this script.
  • Connect SharePoint Online site collection using the PnP module.
  • Get the list of manager names and put in a foreach loop.
  • Then, get the list items using the CAML Query with the respective manager name and copy the information in datatable.
  • Finally, send an email to the respective managers with their reportee person's information

So as my first step, I connect SharePoint Online using PnP module. You can use multiple ways to connect the PnP Online. Here, I am getting the credentials using Get-Credential cmdlets.

  1. Connect-PnPOnline -Url https://tenant.sharepoint.com -Credentials(Get-Credential)

Then, collect the Manager Names using Get-PnPListitem. Here, one more thing I have noticed while doing the test that PnP module is not recognizing the space between column names. For example, if I put a column name as Manager Name, then it’s not giving the result whereas if I use ManagerName, then it's showing the result for me.

  1. $Manager = (Get-PnPListItem -List "Employee" -Fields "MName").FieldValues

Then, I put a foreach loop to repeat the action for all manager names. So, as the next step, I put Get-PnPListIem with CAML Query and I added the respective manager name in the CAML Query. Then only it will filter the list item based on the query.

  1. $Manager = (Get-PnPListItem -List "Employee" -Fields "MName").FieldValues
  2. foreach($name in $Manager)
  3. {
  4. $managername = $name["MName"]
  5. # remaining code will be added
  6. }

Now, I am iterating the list item using Get-PnPlistitem cmdlets with CAML Query and put CAML query like ManagerName column="ManagerName", then only it will filter the respective manager items.

  1. <view><Query><Where><Eq><FieldRef Name='MName' /><Value Type='Text'>managername</Value></Eq></Where></Query></view>
Now, let us copy all the list items in datatable called "listitems" and move that datatable to $database variable. Then, put the foreach loop to add the rows and fianlly, add that $database to dataview and copy to $report variable.
  1. $listitem = Get-PnPListItem -List "Employee" -Query "<view><Query><Where><Eq><FieldRef Name='MName' /><Value Type='Text'>$managername</Value></Eq></Where></Query></view>"
  2. $data = $listitem.FieldValues
  3. #Create DataTable Column Header
  4. $database = New-Object System.Data.DataTable("listitems")
  5. $Column = @("Employee ID","Employee Name","Department","Manager Name")
  6. #Adding column
  7. foreach($header in $Column)
  8. {
  9. $database.columns.Add($header) | Out-Null
  10. }
  11. #Adding Row
  12. foreach($item in $data)
  13. {
  14. $value = $database.NewRow()
  15. # foreach($header in $Column)
  16. # {
  17. $value["Employee ID"] = $item.Title
  18. $value["Employee Name"] =$item.Employee_x0020_Name
  19. $value["Department"] =$item.Department
  20. $value["Manager Name"] =$item.Manger_x0020_Name
  21. # }
  22. $database.Rows.Add($value) | Out-Null
  23. $listid = @($item.ID)
  24. }
  25. $report = New-Object System.Data.DataView($database)
And as the last step, we will be designing the email body. If you are using fully cloud-hosted Office 365, then there is no need to provide the sender email ID and password to authenticate through the Exchange Server. If you are using any hybrid infrastructure, then you need to provide a sender email ID with the password to authenticate with the Exchange Server.
  1. Function sendemail($requestor,$listid)
  2. {
  3. try
  4. {
  5. $body = "<Table border=1><tr><th>Employee ID</th><th>Employee Name</th><th>Department</th><th>Manager Name</th></tr>"
  6. foreach($content in $report)
  7. {
  8. $body += "<tr><td>" + $content[0] +"</td><td>" + $content[1] + "</td><td>" + $content[2] +"</td><td>" + $content[3] +"</td></tr>"
  9. }
  10. $body += "</table><br>"
  11. $emailto = $requestor
  12. $emailsubject ="Your Employee List"
  13. $emailbody = $body
  14. $emailsign = " Thanks <br> Thivagar Segar"
  15. $emaicontent = $emailbody+$emailsign
  16. $emailusername ="sender emailID"
  17. $emailpassword ="Sender Password"
  18. Send-PnPMail -to $requestor -From $emailusername -Subject $emailsubject -Body $emaicontent -Password $emailpassword
  19. Write-Host "Email sent to $requestor" -ForegroundColor Green
  20. }
  21. catch
  22. {
  23. Write-Host "Email Failed due to:"$_.Exception.Message.ToString()
  24. }
  25. }
Here is the full script which will get the list items based on the manager name and send an email to the respective reporting manager.
  1. Clear-Host
  2. try
  3. {
  4. #Connect SharePoint
  5. Connect-PnPOnline -Url https://tenant.sharepoint.com -Credentials(Get-Credential)
  6. Write-Host " **** SharePoint Connected ****" -ForegroundColor Green
  7. }
  8. catch
  9. {
  10. Write-Host "Failed to connect sharepoint" -ForegroundColor Red
  11. }
  12. #Send Email
  13. Function sendemail($requestor,$listid)
  14. {
  15. try
  16. {
  17. $body = "<Table border=1><tr><th>Employee ID</th><th>Employee Name</th><th>Department</th><th>Manager Name</th></tr>"
  18. foreach($content in $report)
  19. {
  20. $body += "<tr><td>" + $content[0] +"</td><td>" + $content[1] + "</td><td>" + $content[2] +"</td><td>" + $content[3] +"</td></tr>"
  21. }
  22. $body += "</table><br>"
  23. $emailto = $requestor
  24. $emailsubject ="Your Employee List"
  25. $emailbody = $body
  26. $emailsign = " Thanks <br> Thivagar Segar"
  27. $emaicontent = $emailbody+$emailsign
  28. $emailusername ="sender emailID"
  29. $emailpassword ="Sender Password"
  30. Send-PnPMail -to $requestor -From $emailusername -Subject $emailsubject -Body $emaicontent -Password $emailpassword
  31. Write-Host "Email sent to $requestor" -ForegroundColor Green
  32. }
  33. catch
  34. {
  35. Write-Host "Email Failed due to:"$_.Exception.Message.ToString()
  36. }
  37. }
  38. #Get Managers name
  39. $Manager = (Get-PnPListItem -List "Employee" -Fields "MName").FieldValues
  40. foreach($name in $Manager)
  41. {
  42. $managername = $name["MName"]
  43. #Iterate List item based on the Manager Namme
  44. $listitem = Get-PnPListItem -List "Employee" -Query "<view><Query><Where><Eq><FieldRef Name='MName' /><Value Type='Text'>$managername</Value></Eq></Where></Query></view>"
  45. $data = $listitem.FieldValues
  46. #Create DataTable Column Header
  47. $database = New-Object System.Data.DataTable("listitems")
  48. $Column = @("Employee ID","Employee Name","Department","Manager Name")
  49. #Adding column
  50. foreach($header in $Column)
  51. {
  52. $database.columns.Add($header) | Out-Null
  53. }
  54. #Adding Row
  55. foreach($item in $data)
  56. {
  57. $value = $database.NewRow()
  58. $value["Employee ID"] = $item.Title
  59. $value["Employee Name"] =$item.Employee_x0020_Name
  60. $value["Department"] =$item.Department
  61. $value["Manager Name"] =$item.Manger_x0020_Name
  62. $database.Rows.Add($value) | Out-Null
  63. $listid = @($item.ID)
  64. }
  65. $report = New-Object System.Data.DataView($database)
  66. if($report.Count -gt 0)
  67. {
  68. $report
  69. sendemail("Enter Manager email ID Here")
  70. $database.Clear()
  71. }
  72. else
  73. {
  74. Write-Host "No Data Found" -ForegroundColor Red
  75. Write-Host "Exit" -ForegroundColor Yellow
  76. }
  77. }
The final output would be like the below image.
Send consolidated list item to the recipient using PnP PowerShell
And, the email body will be like this.
Send consolidated list item to the recipient using PnP PowerShell