SharePoint Online:如何使用 PowerShell 更新文档属性?
要求:使用 PowerShell 更新 SharePoint Online 中文件的元数据。
如何在SharePoint Online中批量编辑元数据?
SharePoint Online 的主要功能之一是能够存储库中每个文档的元数据或属性。这些属性提供有关文档的宝贵信息,例如其状态、作者和创建日期。在本文中,我们将介绍如何使用 PowerShell 更新 SharePoint Online 中的文档属性。这对于更新具有相同属性值的大量文档,或者作为工作流或过程的一部分自动更新文档属性非常有用。
如果要一次更改 SharePoint Online 文档库中多个文档的属性,可以使用现代体验中的详细信息窗格批量编辑文件元数据。
请按照以下步骤批量更新文档属性:
- 浏览 SharePoint Online 文档库 >> 从文档库中选择文档。
- 单击小“i”按钮打开详细信息窗格。
- 现在,您可以批量更改所选文档的属性。
- 单击保存以应用更改。
在 SharePoint Online 中批量编辑元数据:使用“快速编辑”在类似 Excel 的数据表视图中一次性编辑和更新多个文件的元数据!
SharePoint Online:用于更新文件元数据的 PowerShell
让我向您展示如何使用 PowerShell 更新 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 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 为 SharePoint Online 文档库中的所有文件设置元数据值
使用 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"
#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
}
PnP PowerShell 设置文件元数据
以下是如何将文件上传到 SharePoint Online 文档库并使用 PnP PowerShell 设置其元数据:
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/branding"
$FilesPath = "C:\Temp\Technical Design.docx"
$SiteRelativePath = "/documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File from the local path
$File = Get-ChildItem -Path $FilesPath
#Get Created on, Last Modified Properties of the file
$Created = [DateTime]$File.CreationTime
$Modified = [DateTime]$File.LastWriteTime
#Upload the file to SharePoint Online folder - and Set Metadata
Add-PnPFile -Path $File.FullName -Folder $SiteRelativePath -Values @{"Title" = $($File.Name);"Created"=$Created;"Modified"=$Modified}
同样,要设置现有文件的元数据,请使用:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$SiteRelativeURL = "/Branding/Technical Design.docx"
$CreatedBy= "Salaudeen@crescent.com"
$Title = "New Design Document V2.docx"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File from SharePoint
$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem
#Update document properties
Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy}
包起来
总之,使用 PowerShell 更新 SharePoint Online 中的文档属性可以是管理平台内容的强大而有效的方法。通过使用 CSOM PowerShell 脚本或 PnP PowerShell cmdlet,您可以将文档属性更新作为工作流或过程的一部分自动更新,或者使用相同的属性值更新大量文档。
如果要从 CSV 文件批量编辑文档属性,请参阅:SharePoint Online:使用 PowerShell 从 CSV 文件批量更新元数据

