Búsqueda de sitios web

SharePoint Online: actualizar las propiedades del archivo mediante PowerShell


Requisito: PowerShell para actualizar los metadatos de un archivo en SharePoint Online.

PowerShell para actualizar las propiedades de un archivo en SharePoint Online:

Actualizar las propiedades de los documentos en Microsoft SharePoint Online puede ser una tarea tediosa, especialmente si necesita actualizar varias propiedades para varios documentos. En esta publicación de blog, le mostraremos cómo configurar metadatos de un documento en SharePoint Online usando PowerShell.

Actualicemos el valor de la columna "Estado" de un archivo en particular en una biblioteca de documentos de SharePoint Online:


#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
   }

y el resultado:

Este script de PowerShell establece el valor de la columna de metadatos del documento dado. ¿Cómo actualizar la propiedad de metadatos de todos los archivos en una biblioteca de documentos?

PowerShell para actualizar la propiedad de metadatos de todos los documentos de una biblioteca:

Establezcamos un valor de columna para todos los documentos en la biblioteca de documentos de SharePoint Online usando 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="Branding"
$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>            
        </Query>
        <RowLimit Paged='TRUE'>$BatchSize</RowLimit>
</View>
"@

$Counter = 1
#Get Files from the Library in Batches
Do {    
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
      
    #Update column value of the List Item 
    ForEach($Item in $ListItems)
    {
        #Fiter Files
        If($Item.FileSystemObjectType -eq "File")
        {
            #Update List Item "Status" Column
            $Item["Status"]="Completed"
            $Item.Update()
            $Ctx.ExecuteQuery()
        }
        Write-host "Updated Item $Counter of $($List.Itemcount)"
        $Counter++
    }
    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
}While($Query.ListItemCollectionPosition -ne $null)

Aquí hay otra publicación sobre la actualización de metadatos de documentos usando PnP PowerShell, ¿Cómo actualizar metadatos de documentos en SharePoint Online usando PowerShell?

Artículos relacionados