Skip to content

Instantly share code, notes, and snippets.

@OlafD
Created August 12, 2020 09:00
Show Gist options
  • Save OlafD/7b30823abab7785010ed55a2f6169d2e to your computer and use it in GitHub Desktop.
Save OlafD/7b30823abab7785010ed55a2f6169d2e to your computer and use it in GitHub Desktop.
In case for a SharePoint list a lot of list items are needed for testing (eg. for threshold problems), this script could be used. Be sure to use the correct fieldnames in the for-loop. The items will be commited in batches, to speed-up the process.
param (
[string]$Url,
[string]$List,
[int]$NumberOfItems,
[int]$StartAt = 1,
[int]$CommitAfter = 100,
$Credentials
)
if ($Credentials -eq $null)
{
$Credentials = Get-Credential
}
Connect-PnPOnline -Url $Url -Credentials $Credentials
$list = Get-PnPList -Identity $List
$endAt = $NumberOfItems + $StartAt - 1
Write-Host -ForegroundColor Yellow "Will create items starting at $StartAt and ending at $endAt"
Write-Host -ForegroundColor Yellow "Commit after every $commitAfter items."
$commitNo = 0
for ($i = $StartAt; $i -le $EndAt; $i++)
{
$userId = "{0:10000000}" -f $i
$firstname = "Firstname$i"
$lastname = "Lastname$i"
$username = "$firstname $lastname"
$usermail = "$firstname.$lastname@whereever.com"
Write-Host "$userId -- $username"
# $item = Add-PnPListItem -List $list -Values @{ Title = $userId; Username = $username; Usermail = $usermail }
$lici = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$item = $list.AddItem($lici)
$item["Title"] = $userId
$item["Username"] = $username
$item["Usermail"] = $usermail
$item.Update()
$commitNo++
if ($commitNo -ge $CommitAfter)
{
Write-Host "=> Commit"
Invoke-PnPQuery
$commitNo = 0
}
}
Write-Host "=> Final commit"
Invoke-PnPQuery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment