SharePoint Online: Bulk Update All Items in Large List using PowerShell

Requirement: Bulk Update SharePoint Online List Items using PowerShell.
PowerShell to Bulk Update All Items in Large List in SharePoint Online

How to Update a Large Number of List Items in Bulk using PowerShell?
While lists < 5000 items can be updated using the approach in my other post, Update SharePoint Online List using PowerShell, How about updating large lists with more than 5000 list items?

Well, large lists must be updated in batches. Here is my PowerShell script to update large lists in bulk: This PowerShell script updates the "Title" field value from "File Name".
#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"
  
#Parameters
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Documents"
$UserName = "[email protected]"
$Password = "Password123456"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
$BatchSize =100
 
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()

#sharepoint online powershell caml batch update
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
    <View Scope='RecursiveAll'> 
        <Query> 
             <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit>$BatchSize</RowLimit>
    </View>"

#Get List Items in Batches
Do
{
    #Get List Items 
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
    $ListItems.Count

    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    $Query.ListItemCollectionPosition

    If ($ListItems.Count -eq 0) { Break }
    
    #Update List Item
    ForEach($Item in $ListItems)
    {
        #Update List Item Title as File Name
        $Item["Title"]= $Item["FileLeafRef"]
        $Item.Update()
    }
    $Ctx.ExecuteQuery()
}While ($Query.ListItemCollectionPosition -ne $null)

PnP PowerShell to Update All Items in a SharePoint Online List
Updating multiple list items using PnP PowerShell is relatively simpler than the above CSOM script (That's the Power of PnP!).
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"

#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -UseWebLogin

#Get all Items from the list
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000

#Update all list Items
ForEach($Item in $ListItems)
{
    #Update List Item Title as its ID value
    Set-PnPListItem -List $ListName -Identity $Item.Id -Values @{"Title"= $Item.Id} | Out-Null
    Write-host "Updated Item:"$Item.ID    
}

If you want to bulk update multiple items from a CSV file, use: Update SharePoint Online List from a CSV using PowerShell

3 comments:

  1. I haven't been able to get this to work on lists over 5000. On lists under 5000 items, I see that it processes items in batches of 100 and have verified that the items have been updated. On a list over 5000, I get the dreaded "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator"

    ReplyDelete
    Replies
    1. Script has been updated, Try now!

      Delete
    2. This now works. Thank you so much!

      I'm not very strong in the Power Shell world yet, I truly appreciate your help.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.