update metadata sharepoint document library using PowerShell

Safely SharePoint update metadata in document library using PowerShell CSOM – O365

3 comments

Loading

In this “SharePoint update metadata in the document library using PowerShell” post, I will show how to update SharePoint online document library metadata or column value in a batch using PowerShell CSOM code programmatically.

SharePoint update document library metadata using PowerShell CSOM

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.

SharePoint update document library metadata - SharePoint Online

Update document library metadata using PowerShell CSOM: SharePoint PowerShell update metadata

Use the below code to update document library metadata using PowerShell in SharePoint Online:

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 “Approved” status now changed to “Closed” status.

SharePoint PowerShell update metadata demo

Summary: SharePoint update document library metadata (update metadata SharePoint document library using PowerShell)

Thus, in this article, we have learned how to update document library metadata in SharePoint Online using PowerShell.

See Also: SharePoint Online PowerShell

You may also like the below SharePoint Online PowerShell tutorials:

Buy SharePoint Online & Microsoft 365 Administration eBook

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

About Post Author

3 comments on “Safely SharePoint update metadata in document library using PowerShell CSOM – O365”

Do you have a better solution or question on this topic? Please leave a comment