ค้นหาเว็บไซต์

SharePoint Online: จะอัปเดตคุณสมบัติเอกสารโดยใช้ PowerShell ได้อย่างไร


ความต้องการ: อัปเดตข้อมูลเมตาของไฟล์ใน SharePoint Online โดยใช้ PowerShell

วิธีแก้ไขข้อมูลเมตาจำนวนมากใน SharePoint Online

หนึ่งในฟีเจอร์หลักของ SharePoint Online คือความสามารถในการจัดเก็บข้อมูลเมตาหรือคุณสมบัติสำหรับแต่ละเอกสารในไลบรารี คุณสมบัติเหล่านี้ให้ข้อมูลที่เป็นประโยชน์เกี่ยวกับเอกสาร เช่น สถานะ ผู้เขียน และวันที่สร้าง ในบทความนี้ เราจะกล่าวถึงวิธีการอัปเดตคุณสมบัติเอกสารใน SharePoint Online โดยใช้ PowerShell สิ่งนี้มีประโยชน์สำหรับการอัพเดตเอกสารจำนวนมากที่มีค่าคุณสมบัติเดียวกัน หรือสำหรับการอัพเดตคุณสมบัติเอกสารโดยอัตโนมัติโดยเป็นส่วนหนึ่งของเวิร์กโฟลว์หรือกระบวนการ

หากคุณต้องการเปลี่ยนคุณสมบัติของเอกสารหลายฉบับในไลบรารีเอกสาร SharePoint Online พร้อมกัน คุณสามารถใช้บานหน้าต่างรายละเอียดในประสบการณ์การใช้งานสมัยใหม่เพื่อแก้ไขข้อมูลเมตาของไฟล์จำนวนมากได้

ทำตามขั้นตอนเหล่านี้เพื่ออัพเดตคุณสมบัติเอกสารจำนวนมาก:

  1. เรียกดูไลบรารีเอกสาร SharePoint Online ของคุณ >> เลือกเอกสารจากไลบรารีเอกสาร
  2. คลิกที่ปุ่ม “i” เล็กๆ เพื่อเปิดบานหน้าต่างรายละเอียด
  3. ตอนนี้คุณสามารถเปลี่ยนคุณสมบัติของเอกสารที่เลือกเป็นกลุ่มได้
  4. คลิกบันทึกเพื่อใช้การเปลี่ยนแปลง

แก้ไขข้อมูลเมตาเป็นกลุ่มใน 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}

ห่อ

โดยสรุป การอัปเดตคุณสมบัติเอกสารใน SharePoint Online โดยใช้ PowerShell อาจเป็นวิธีที่มีประสิทธิภาพและมีประสิทธิภาพในการจัดการเนื้อหาในแพลตฟอร์ม ด้วยการใช้สคริปต์ CSOM PowerShell หรือ PnP PowerShell cmdlets คุณสามารถอัปเดตคุณสมบัติเอกสารโดยอัตโนมัติโดยเป็นส่วนหนึ่งของเวิร์กโฟลว์หรือกระบวนการ หรืออัปเดตเอกสารจำนวนมากด้วยค่าคุณสมบัติเดียวกัน

หากคุณต้องการแก้ไขคุณสมบัติเอกสารจำนวนมากจากไฟล์ CSV โปรดดู: SharePoint Online: อัปเดตข้อมูลเมตาจำนวนมากจากไฟล์ CSV โดยใช้ PowerShell

บทความที่เกี่ยวข้อง