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

Requirement: Bulk Update SharePoint Online List Items using PowerShell

How to Update Large Number of List Items in Bulk using PowerShell?
While lists < 5000 items can be updated using the approach in my another 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 = "Salaudeen@CrescentTech.com"
$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()

#Define CAML Query to get Files from the list in batches
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
    <View Scope='RecursiveAll'> 
        <Query> 
            <Where>
                    <Eq>
                        <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                    </Eq>
            </Where> 
        </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)

SharePoint Online: Bulk Update All Items in Large List using PowerShell SharePoint Online: Bulk Update All Items in Large List using PowerShell Reviewed by Salaudeen Rajack on June 14, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.