How to update SharePoint online document library field value using PowerShell CSOM?

No comments

In this post, I will show that how to update SharePoint online document library field value in a batch using PowerShell CSOM.

This is my  SharePoint online document library which has “DocumentStatus” column with the various status where I will change the document status to Closed if the document is approved.

UpdateDocumentLibrayColumnValue

PowerShell Script:

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 = "@
    <View Scope='RecursiveAll'>
        <Query>
             <OrderBy><FieldRef Name='ID' Ascending='True'/></OrderBy>
             
        </Query>
        <RowLimit>$BatchSize</RowLimit>
    </View>"
$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.

 

UpdateDocumentLibrayColumnValue3