SharePoint Online: คัดลอกโครงสร้างโฟลเดอร์ระหว่างไลบรารีเอกสารโดยใช้ PowerShell
ความต้องการ: คัดลอกโครงสร้างโฟลเดอร์ระหว่างไลบรารีเอกสารใน SharePoint Online
PowerShell เพื่อคัดลอกโครงสร้างโฟลเดอร์ระหว่างไลบรารีเอกสาร SharePoint Online:
เคยต้องการโคลนโฟลเดอร์ โครงสร้างโฟลเดอร์ย่อยระหว่างไลบรารีเอกสาร 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"
#Function to Copy All Folders from Source to Destination
Function Copy-SPOFolder
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
$Ctx = $TargetFolder.Context
#Get all Folders from the Source
$SubFolders = $SourceFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
Try {
$Folder=$Ctx.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$Ctx.load($Folder)
$Ctx.ExecuteQuery()
Write-host "Folder Already Exists:"$TargetFolderURL -f Yellow
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$Folder=$Ctx.Web.Folders.Add($TargetFolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host "Folder Copied:"$TargetFolderURL -f Green
}
}
#Call the function recursively
Copy-SPOFolder -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#Function to Copy Folder Structure between document libraries
Function Copy-SPOFolderStructure
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceLibraryName,
[Parameter(Mandatory=$true)] [string] $TargetLibraryName
)
Try {
#Setup 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 source library and Target Libraries
$SourceLibrary = $Ctx.Web.Lists.GetByTitle($SourceLibraryName)
$Ctx.Load($SourceLibrary)
$Ctx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $Ctx.Web.Lists.GetByTitle($TargetLibraryName)
$Ctx.Load($TargetLibrary)
$Ctx.Load($TargetLibrary.RootFolder)
$Ctx.ExecuteQuery()
#Call the function to copy folders from source library to the target
Copy-SPOFolder -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceLibraryName="Team Documents"
$TargetLibraryName="Project Documents"
#Call the function
Copy-SPOFolderStructure -SiteURL $SiteURL -SourceLibraryName $SourceLibraryName -TargetLibraryName $TargetLibraryName
สคริปต์นี้คัดลอกโฟลเดอร์และโฟลเดอร์ย่อยทั้งหมดจากไลบรารีต้นทางไปยังเป้าหมาย หากคุณต้องการคัดลอกโฟลเดอร์พร้อมกับไฟล์ ให้ใช้: SharePoint Online: คัดลอกไฟล์ระหว่างไลบรารีเอกสารโดยใช้ PowerShell
