当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 更新文件属性

作者:精品下载站 日期:2024-12-14 14:43:30 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 更新文件属性


要求: PowerShell 更新 SharePoint Online 中文件的元数据。

使用 PowerShell 更新 SharePoint Online 中文件的属性:

在 Microsoft SharePoint Online 中更新文档属性可能是一项繁琐的任务,尤其是当您需要更新多个文档的多个属性时。在这篇博文中,我们将向您展示如何使用 PowerShell 在 SharePoint Online 中设置文档的元数据。

让我们更新 SharePoint Online 文档库中特定文件的“状态”列值:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
   }

结果:

[玩转系统] SharePoint Online:使用 PowerShell 更新文件属性

此 PowerShell 脚本设置给定文档的元数据列值。如何更新文档库中所有文件的元数据属性?

PowerShell 更新库中所有文档的元数据属性:

让我们使用 PowerShell 为 SharePoint Online 文档库中的所有文档设置一个列值。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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)

这是关于使用 PnP PowerShell 更新文档元数据的另一篇文章,如何使用 PowerShell 更新 SharePoint Online 中的文档元数据?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯