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.
- CLS
- #Load SharePoint CSOM Assemblies
- #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
- #Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- $fileName = "Adding_Multiple_Items_Report"
- $enddate = (Get-Date).tostring("yyyyMMddhhmmss")
- $logFileName = $fileName +"_"+ $enddate+"_Log.txt"
- $invocation = (Get-Variable MyInvocation).Value
- $directoryPath = Split-Path $invocation.MyCommand.Path
- $directoryPathForLog=$directoryPath+"\"+"LogFiles"
- if(!(Test-Path -path $directoryPathForLog))
- {
- New-Item -ItemType directory -Path $directoryPathForLog
- }
- $logPath = $directoryPathForLog + "\" + $logFileName
- $isLogFileCreated = $False
- #DLL location
- $directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
- if(!(Test-Path -path $directoryPathForDLL))
- {
- New-Item -ItemType directory -Path $directoryPathForDLL
- }
- #DLL location
- $clientDLL=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
- $clientDLLRuntime=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
- Add-Type -Path $clientDLL
- Add-Type -Path $clientDLLRuntime
- function Write-Log([string]$logMsg)
- {
- if(!$isLogFileCreated){
- Write-Host "Creating Log File..."
- if(!(Test-Path -path $directoryPath))
- {
- Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
- }
- else
- {
- $script:isLogFileCreated = $True
- Write-Host "Log File ($logFileName) Created..."
- [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
- Add-Content -Path $logPath -Value $logMessage
- }
- }
- else
- {
- [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
- Add-Content -Path $logPath -Value $logMessage
- }
- }
- #variables region.
- $siteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/" #Your Site URL
- $spUserName="YourUserName@<yourtenantname>.com"
- $password = "YourPassword"
- $spListName="Test List"
- $numberOfItemsToCreate="5001"
- #variables region end.
- $securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
- #Setup the Context
- try
- {
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spUserName, $securePassword)
- #Get the list
- $spList = $ctx.Web.Lists.GetByTitle($spListName)
- $ctx.Load($spList)
- $ctx.ExecuteQuery()
- # Loop to create list items
- for($i=1; $i -le $numberOfItemsToCreate; $i++)
- {
- $listItemCreationInformationInSPOnline = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
- $newListItemInSPOnline = $spList.AddItem($listItemCreationInformationInSPOnline)
- $newListItemInSPOnline["Title"] = "CustomItemNumberAddedThruCode_$($i)"
- $newListItemInSPOnline["CustomItemNumber"] = $i;
- $newListItemInSPOnline.Update()
- $ctx.ExecuteQuery()
- write-host "Item created: CustomItemNumberAddedThruCode_$($i)"
- }
- }
- catch
- {
- $errorMessage = $_.Exception.Message +"in adding mulitple items in SP Online list using CSOM PowerShell script";
- Write-Host $errorMessage -BackgroundColor Red
- Write-Log $errorMessage
- }
- Write-Host "##########################################################" -ForegroundColor Green
- Write-Host "The script execution has been completed!" -ForegroundColor Green
- 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:
- #variables region.
- $siteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/" #Your Site URL
- $spUserName="YourUserName@<yourtenantname>.com"
- $password = "YourPassword"
- $spListName="Your List Name"
- $numberOfItemsToCreate="Number of times you want to create(Number like - 1000,2000,3000 etc)"
- #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.

Join the conversation! Your thoughts help the community grow.