24,814 total views, 46 views today
In this post, I will show how to update SharePoint online document library column or metadata value in a batch using PowerShell CSOM code
programmatically.
This is my SharePoint online document library which has the “DocumentStatus” column with the various status where I will change the document status to Closed if the document is approved.
Update document library metadata using PowerShell CSOM
CLS #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://globalsharepoint.sharepoint.com/sites/TestSite/" $ListName="Documents" $UserName = "yourusername@globalsharepoint.onmicrosoft.com" $Password = "ThisIsTestPassword" $SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force $BatchSize =1000 #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() $emptyString = "" #Define CAML Query to get Files from the list in batches $Query = New-Object Microsoft.SharePoint.Client.CamlQuery $Query.ViewXml = "@ $BatchSize " $count =0 #Get List Items in Batches Do { $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 $documentStatus=$Item["DocumentStatus"] If($documentStatus -eq "Approved") { $test=$Item; $file=$Item.File; #Get the List Item with all metadata fields of the File #$ListItem = $file.ListItemAllFields #$Ctx.Load($file) #$Ctx.ExecuteQuery() $Item["DocumentStatus"]="Closed" $Item.Update(); $Ctx.ExecuteQuery() } } Write-Host "=============================================================" Write-Host $count Write-Host "=============================================================" }While ($Query.ListItemCollectionPosition -ne $null)
After executing the above script we can see whichever items had with “Approved” status now changed to “Closed” status.
See Also:
- Get hub sites and associated sites using PnP PowerShell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- SharePoint Automation: PowerShell script to get remote server information
- SharePoint Online Automation – O365 – Send email from SharePoint online Using CSOM PowerShell script
- SharePoint Online Automation – O365 – Download files from a document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
3 comments on “SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM”
You must log in to post a comment.