SharePoint Online: อัปโหลดไฟล์ไปยังโฟลเดอร์ย่อยโดยใช้ PowerShell
ข้อกำหนด: SharePoint Online PowerShell เพื่ออัปโหลดไฟล์ไปยังโฟลเดอร์
จะอัพโหลดไฟล์ไปยังโฟลเดอร์ใน SharePoint Online ได้อย่างไร
โฟลเดอร์ยังคงเป็นวิธีที่ยอดเยี่ยมในการจัดระเบียบไฟล์ของคุณและเก็บไฟล์ทั้งหมดไว้ในที่เดียว ในโพสต์บนบล็อกนี้ ฉันจะแสดงวิธีใช้ PowerShell เพื่ออัปโหลดไฟล์ไปยังโฟลเดอร์ในไซต์ SharePoint Online สิ่งนี้มีประโยชน์ถ้าคุณต้องการทำให้กระบวนการอัพโหลดไฟล์ไปยังไซต์ SharePoint ของคุณเป็นแบบอัตโนมัติ
คุณสามารถอัปโหลดไฟล์ไปยังโฟลเดอร์เฉพาะในไลบรารีเอกสาร SharePoint Online ได้โดย:
- นำทางไปยังโฟลเดอร์ผ่านเว็บเบราว์เซอร์
- คลิกที่ปุ่ม "อัปโหลด" ในแถบเครื่องมือของไลบรารีเอกสาร เลือก "ไฟล์" จากนั้นเรียกดูไฟล์ที่จัดเก็บไว้ในเครื่องในคอมพิวเตอร์ของคุณ
- ค้นหาไฟล์ที่ต้องการ เลือกไฟล์ คลิกเปิด จากนั้นการอัปโหลดจะเริ่มขึ้น
เมื่อกระบวนการอัปโหลดเสร็จสมบูรณ์ แถบความคืบหน้าที่ด้านบนจะถึง 100%
PowerShell เพื่ออัปโหลดไฟล์ไปยังโฟลเดอร์ย่อยของ SharePoint Online
PowerShell เป็นภาษาที่ทรงพลังอย่างน่าอัศจรรย์ที่สามารถใช้เพื่อทำให้งานไอทีหลายอย่างของคุณเป็นอัตโนมัติ งานทั่วไปอย่างหนึ่งที่คุณอาจต้องการทำให้เป็นอัตโนมัติด้วย PowerShell คือการอัปโหลดไฟล์ไปยังโฟลเดอร์ย่อยไลบรารีเอกสาร SharePoint Online
บ่อยครั้งที่คุณอาจต้องอัปโหลดไฟล์ไปยัง 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 parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceFilePath="C:\Users\salaudeen\Desktop\Project Documents\Discloser Asia.doc"
$TargetFolderRelativeURL ="/sites/Marketing/Shared Documents/2018/Active"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Target Folder to upload
$Web = $Ctx.Web
$Ctx.Load($Web)
$TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Get the source file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFilePath -leaf
$TargetFileURL = $TargetFolderRelativeURL+"/"+$SourceFileName
#Upload the File to SharePoint Library Folder
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $TargetFileURL
$FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
}
catch {
write-host "Error Uploading File to Folder: $($_.Exception.Message)" -foregroundcolor Red
}
PowerShell นี้จะอัปโหลดไฟล์ไปยังโฟลเดอร์ย่อยไลบรารีเอกสาร SharePoint Online
อัปโหลดไฟล์ไปยังโฟลเดอร์ SharePoint Online โดยใช้ PnP PowerShell
หากต้องการอัปโหลดไฟล์ไปยังโฟลเดอร์ในไลบรารีเอกสาร SharePoint Online โดยใช้ PnP PowerShell ให้ใช้:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/retail"
$SourceFilePath ="C:\Documents\SRS.Docx"
$DestinationFolderPath = "/sites/retail/Shared Documents/2018" #Server Relative URL
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Upload File to Folder
Add-PnPFile -Path $SourceFilePath -Folder $DestinationFolderPath -ErrorAction Stop
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Add-PnPFile cmdlet ยังรองรับพารามิเตอร์เหล่านี้: NewFileName, Checkout, Approve, Publish, ContentType และ Values หากคุณต้องการอัปโหลดไฟล์และโฟลเดอร์ทั้งหมดจากไดรฟ์ในเครื่องไปยัง SharePoint Online โดยใช้ PowerShell ให้ใช้: SharePoint Online: อัปโหลดไฟล์และโฟลเดอร์ย่อยทั้งหมดโดยใช้ PowerShell
