Get, Update, Add, Remove SharePoint list items in large lists
Summary
Working and processing lists items in large lists. PnP PowerShell and M365 CLI examples
Implementation
- Open Windows PowerShell ISE
- Create a new file
- Copy a script below,
$url = "https://yourtenantname.sharepoint.com/sites/SiteCollection"
$list = "YourLargeList"
Connect-PnPOnline -Url $Url -Interactive
# create 5000+ list items
$batch = New-PnPBatch
1..5500 | ForEach-Object {
Add-PnPListItem -List $list -Values @{"Title"="Test Item Batched $_"} -Batch $batch
}
Invoke-PnPBatch -Batch $batch
#Update each list item separatelly
$batch = New-PnPBatch
$items = Get-PnPListItem -List $list -PageSize 1000
$items | ForEach-Object {
Set-PnPListItem -List $list -Identity $_.Id -Values @{"Title"="Test Item Batched and updated $_"} -Batch $batch
}
Invoke-PnPBatch -Batch $batch
#remove each list item separatelly
$batch = New-PnPBatch
$items = Get-PnPListItem -List $list -PageSize 1000
$items | ForEach-Object {
Remove-PnPListItem -List $list -Identity $_.Id
}
Invoke-PnPBatch -Batch $batch
#read each list item separatelly
$batch = New-PnPBatch
Get-PnPListItem -List $list -PageSize 1000 | ForEach-Object {
get-PnPListItem -List $list -Identity $_
}
Invoke-PnPBatch -Batch $batch
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Valeras Narbutas |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.