SharePoint Online: How to Update Document Metadata using PowerShell?

Requirement: Update Metadata of a File in SharePoint Online using PowerShell
Update Document Metadata in SharePoint Online using PowerShell

SharePoint Online: PowerShell to Update Metadata of a File
#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 Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$FileRelativeURL="/sites/Marketing/Shared Documents/Release Notes.dotx"
 
#Setup Credentials to connect
$Cred = Get-Credential
 
Try {
    #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 File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()

    #Set metadata of the File
    $ListItem = $File.ListItemAllFields
    $ListItem["ReviewTimestamp"] = [System.DateTime]::Now  
    $ListItem.Update()  
    $Ctx.ExecuteQuery()    
 
    write-host -f Green "Document Metadata Updated Successfully!"
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

PowerShell to Set Metadata Value for All Files 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 Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
 
#Setup Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get All Items from the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()       
 
    #Loop through each item 
    ForEach($ListItem in $ListItems)
    {
        #Set metadata of the List Item
        $ListItem["ReviewTimestamp"] = [System.DateTime]::Now
        $ListItem.Update()
        write-host -f Green "Document Metadata Updated Successfully for:" $ListItem["FileLeafRef"]        
    }
    $Ctx.ExecuteQuery()
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}
SharePoint Online: How to Update Document Metadata using PowerShell? SharePoint Online: How to Update Document Metadata using PowerShell? Reviewed by Salaudeen Rajack on July 07, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.