Home SharingIsCaring. Get, Update, Add, Remove SharePoint list items in large lists
Post
Cancel

SharingIsCaring. 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

  1. Open Windows PowerShell ISE
  2. Create a new file
  3. Copy a script below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

$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

Using M365 CLI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56


$url = "Site Url"
$listName = "LargeListTitle"


$m365Status = m365 status
if ($m365Status -match "Logged Out") {
  Write-Host "Logging in the User!"
  m365 login --authType browser
}

#count list items
$listProperties = m365 spo list get --title  $listName --webUrl $url -o json | ConvertFrom-Json
$itemCount = $listProperties.ItemCount

#Set up page size and page number
$pageSize = 1000
$pageNumber = [int][Math]::Ceiling($itemCount/$pageSize)


# get all items from large list
for ($i = 0; $i -lt $pageNumber; $i++)
{ 
  # get items from large library
 m365 spo listitem list --title $listName --webUrl $url --pageSize $pageSize --pageNumber $i  
}


# create list items
1..100 | ForEach-Object { 
            m365 spo listitem add --contentType Item --listTitle $listName --webUrl $url --Title "Demo Item using CLI"
           }

#update list items
for ($i = 0; $i -lt $pageNumber; $i++)
{ 
   $items = m365 spo listitem list --title $listName --webUrl $url --fields "ID"  --pageSize $pageSize --pageNumber $i --output json 
    $items = $items.Replace("Id","Idd") | ConvertFrom-Json
    $items | select -ExpandProperty ID | ForEach-Object { 
             m365 spo listitem set --listTitle $listName --id $_ --webUrl $url --Title "update with cli"
           }
}

#remove list items
for ($i = 0; $i -lt $pageNumber; $i++)
{ 
  # get items from large library
    $items = m365 spo listitem list --title $listName --webUrl $url --fields "ID"  --pageSize $pageSize --pageNumber $i --output json 
    $items = $items.Replace("Id","Idd") | ConvertFrom-Json
    $items | select -ExpandProperty ID | ForEach-Object { 
             m365 spo listitem remove --webUrl $url --listTitle $listName --id $_  --confirm 
           }
}


PNP Script sample site

Script sample site

This post is licensed under CC BY 4.0 by the author.

SharingIsCaring. Extract Space content type Modern page as template and save to SharePoint

Tip Of The Day. Easy Track changes in dataverse table