Introduction

Sometimes we need to create multiple list items at a time in SharePoint list due to a requirement - it could be while architecting the SharePoint application or doing SharePoint application testing. So when it comes to adding thousands of records in a list - it is not possible to add those records manually. Hence, the below script will be very helpful while handling this kind of requirement.
  1. CLS
  2. #Load SharePoint CSOM Assemblies
  3. #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
  4. #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  5. $fileName = "Adding_Multiple_Items_Report"
  6. $enddate = (Get-Date).tostring("yyyyMMddhhmmss")
  7. $logFileName = $fileName +"_"+ $enddate+"_Log.txt"
  8. $invocation = (Get-Variable MyInvocation).Value
  9. $directoryPath = Split-Path $invocation.MyCommand.Path
  10. $directoryPathForLog=$directoryPath+"\"+"LogFiles"
  11. if(!(Test-Path -path $directoryPathForLog))
  12. {
  13. New-Item -ItemType directory -Path $directoryPathForLog
  14. }
  15. $logPath = $directoryPathForLog + "\" + $logFileName
  16. $isLogFileCreated = $False
  17. #DLL location
  18. $directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
  19. if(!(Test-Path -path $directoryPathForDLL))
  20. {
  21. New-Item -ItemType directory -Path $directoryPathForDLL
  22. }
  23. #DLL location
  24. $clientDLL=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
  25. $clientDLLRuntime=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
  26. Add-Type -Path $clientDLL
  27. Add-Type -Path $clientDLLRuntime
  28. function Write-Log([string]$logMsg)
  29. {
  30. if(!$isLogFileCreated){
  31. Write-Host "Creating Log File..."
  32. if(!(Test-Path -path $directoryPath))
  33. {
  34. Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
  35. }
  36. else
  37. {
  38. $script:isLogFileCreated = $True
  39. Write-Host "Log File ($logFileName) Created..."
  40. [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
  41. Add-Content -Path $logPath -Value $logMessage
  42. }
  43. }
  44. else
  45. {
  46. [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
  47. Add-Content -Path $logPath -Value $logMessage
  48. }
  49. }
  50. #variables region.
  51. $siteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/" #Your Site URL
  52. $spUserName="YourUserName@<yourtenantname>.com"
  53. $password = "YourPassword"
  54. $spListName="Test List"
  55. $numberOfItemsToCreate="5001"
  56. #variables region end.
  57. $securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
  58. #Setup the Context
  59. try
  60. {
  61. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
  62. $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spUserName, $securePassword)
  63. #Get the list
  64. $spList = $ctx.Web.Lists.GetByTitle($spListName)
  65. $ctx.Load($spList)
  66. $ctx.ExecuteQuery()
  67. # Loop to create list items
  68. for($i=1; $i -le $numberOfItemsToCreate; $i++)
  69. {
  70. $listItemCreationInformationInSPOnline = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
  71. $newListItemInSPOnline = $spList.AddItem($listItemCreationInformationInSPOnline)
  72. $newListItemInSPOnline["Title"] = "CustomItemNumberAddedThruCode_$($i)"
  73. $newListItemInSPOnline["CustomItemNumber"] = $i;
  74. $newListItemInSPOnline.Update()
  75. $ctx.ExecuteQuery()
  76. write-host "Item created: CustomItemNumberAddedThruCode_$($i)"
  77. }
  78. }
  79. catch
  80. {
  81. $errorMessage = $_.Exception.Message +"in adding mulitple items in SP Online list using CSOM PowerShell script";
  82. Write-Host $errorMessage -BackgroundColor Red
  83. Write-Log $errorMessage
  84. }
  85. Write-Host "##########################################################" -ForegroundColor Green
  86. Write-Host "The script execution has been completed!" -ForegroundColor Green
  87. Write-Host "##########################################################"

How to execute the above script?

Place the below two DLLs in your script directory "Dependency Files" folder as like below:
Change the value of the variable in the variables section like below:
  1. #variables region.
  2. $siteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/" #Your Site URL
  3. $spUserName="YourUserName@<yourtenantname>.com"
  4. $password = "YourPassword"
  5. $spListName="Your List Name"
  6. $numberOfItemsToCreate="Number of times you want to create(Number like - 1000,2000,3000 etc)"
  7. #variables region end.
Execute the above script like below:
After execution - output:
We can see that n($numberOfItemsToCreate) number items are added to the SharePoint list automatically.

Summary

Hence, we have learned here how to create multiples items in SharePoint online in a batch using CSOM PowerShell.