SharePoint Online: Update Metadata of a Document using PowerShell

Requirement: PowerShell to update metadata of a file in SharePoint Online.

PowerShell to Update the Metadata of a File in SharePoint Online:
How to set metadata of a document in SharePoint Online? Let's update the "Status" Column value of a particular file in a SharePoint Online Document Library.
#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"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops"
$FileRelativeUrl="/sites/Ops/Shared Documents/Investment Process.pptx"

Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeUrl)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()

        #Set Metadata of the File
        $ListItem = $File.ListItemAllFields
        $Listitem["Status"] = "Completed"
        $ListItem.Update()
        $Ctx.ExecuteQuery()

        Write-host -f Green "File's Metadata has been Updated Successfully!"
     }
    Catch {
        write-host -f Red "Error Updating Metadata of the File!" $_.Exception.Message
   }
and the result:
powershell to update metadata of a file in sharepoint online
This PowerShell script sets the metadata column value of the given document. How to update metadata property of all files in a document library?

PowerShell to Update Metadata Property of All Documents in a Library:
Let's set a column value for all documents in SharePoint Online document library using PowerShell.
#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://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$BatchSize = 2000

#Get Credentials to connect
$Cred= Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#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>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
            <Where>
                    <Eq>
                        <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                    </Eq>
            </Where>
        </Query>
        <RowLimit Paged='TRUE'>$BatchSize</RowLimit>
</View>"

#Get Files from the Library in Batches
Do {    
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
   
    If ($ListItems.Count -eq 0) { Break; }
     
    #Update column value of the List Item 
    ForEach($Item in $ListItems)
    {
        #Update List Item "Status" Column
        $Item["Status"]="Completed"
        $Item.Update()
        $Ctx.ExecuteQuery()
    }    
} While ($Query.ListItemCollectionPosition -ne $null)
Here is my another post on updating document metadata using PnP PowerShell How to Update Document Metadata in SharePoint Online using PowerShell?

5 comments:

  1. Thanks for this post. How would we update the metadata of files in Library using a CSV file? ie. Update other fields in the library based on an item that is found in CSV file?

    ReplyDelete
  2. I did the same script but my column doesn't appear in Sharepoint.

    I try to did it on also on a folder (which is in fact a documentset type folder). The script pass without error but it doesn't appear in SharePoint

    Any idea ?

    ReplyDelete
    Replies
    1. Hi I found a part of the problem. I added to my metadata a field named "test" which is in plain text. I'm able to update it using the script. However, the field that support multiple selection doesn't work. Do I have to push it as an array ?

      Delete
    2. Exactly! Fields like single lines of text, choice works fine with this script. For other field types such as Date time, Person or group, Managed metadata, You have to handle them differently! E.g. To update Multiple choice column, use:
      $ListItem[$FieldName] = @("Development", "IT Applications", "Real Estate")

      Refer:
      SharePoint Online: How to Update Choice Field Value using PowerShell?

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.