SharePoint Online: PowerShell kullanarak Site Koleksiyonları Arasında Dosya Kopyalama
Gereksinim: Dosyaları SharePoint Çevrimiçi Site Koleksiyonlarındaki belge kitaplıkları arasında kopyalayın.
SharePoint Çevrimiçi Site Koleksiyonları Arasında Dosya Nasıl Kopyalanır?
SharePoint Online'da site koleksiyonları arasında dosya kopyalamanın bir yolunu mu arıyorsunuz? Duruma göre bu birkaç farklı yöntem kullanılarak yapılabilir. Bu blog yazısı, dosyaları bir siteden diğerine manuel olarak kopyalamak için size adım adım talimatlar sağlayacaktır. Süreci otomatikleştirmek için PowerShell'i kullanarak site koleksiyonları arasında dosyaları nasıl kopyalayacağınızı da göstereceğiz.
SharePoint Online'da belge kitaplıkları arasında dosyaların nasıl kopyalanacağı aşağıda açıklanmıştır:
SharePoint Online belge kitaplığınıza gidin >> Kopyalanacak dosyaları seçin >> Araç çubuğunda "Kopyala"ya tıklayın. (veya bir belgeye sağ tıklayıp içerik menüsünden "Kopyala"yı seçebilirsiniz).
-
Bu sağdaki bilgi panelini açar. Dosyanızın kopyalanması gereken hedef kitaplığı seçin. Geçerli kitaplıktaki herhangi bir klasörü, geçerli sitedeki başka herhangi bir kitaplığı ve hatta farklı site koleksiyonlarındaki bir kitaplığı seçebilirsiniz.
- Belgeyi kopyalamak için hedef konumu seçin ve dosyayı kopyalamaya başlamak için “Buraya Kopyala” düğmesine tıklayın.
Araç çubuğunda “Kopyalanıyor” mesajını göreceksiniz ve dosyanız bir süre sonra kopyalanacaktır. Aynı sitedeki kitaplıklar arasında veya farklı site koleksiyonları arasında dosya kopyalamak için bu yöntemi kullanabilirsiniz.
SharePoint Online'da Site Koleksiyonları Arasında Dosya Kopyalamak için PowerShell:
Dosyaları farklı site koleksiyonları arasında hızla aktarmak istiyorsanız PowerShell, SharePoint Online dosyalarını kopyalamanın etkili bir yoludur. PowerShell kullanarak dosyaları kopyalamak için öncelikle hem kaynak hem de hedef sitelere bağlanmanız gerekir. Her iki siteye de bağlandıktan sonra dosyaları kaynak siteden hedef siteye kopyalamak için OpenBinaryDirect ve SaveBinaryDirect CSOM yöntemlerini kullanabilirsiniz.
Tek bir dosyayı kaynaktan hedefe kopyalayalım:
#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
$SourceSiteURL="https://crescent.sharePoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/Ops/"
#Set Source and Destination File URLs - Relative path
$SourceFileURL="/Sites/Sales/Project Documents/Active Users.xlsx"
$TargetFileURL="/Sites/Ops/Shared Documents/Active Users.xlsx"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the Source File
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceCtx, $SourceFileURL)
#Copy File to the Target location
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetCtx, $TargetFileURL, $FileInfo.Stream,$True)
Bu komut dosyası, belirli bir dosyayı kaynak site/site koleksiyonundan hedef site/site koleksiyonuna kopyalar.
Tüm Dosyaları ve Klasörü Farklı Siteler arasında Bir Kitaplıktaki Diğer Kitaplığa Kopyalama:
PowerShell, SharePoint siteleri arasında dosya kopyalama da dahil olmak üzere birçok görevi otomatikleştirmek için kullanılabilecek güçlü bir betik dilidir. SharePoint Online'da tüm dosya ve klasörleri bir kitaplıktan diğerine nasıl kopyalayacağınız aşağıda açıklanmıştır:
#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 Copy-Files
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$SourceFolder.Context.Load($SourceFilesColl)
$SourceFolder.Context.ExecuteQuery()
#Iterate through each file and copy
Foreach($SourceFile in $SourceFilesColl)
{
#Get the source file
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)
#Copy File to the Target location
$TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)
Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$SourceFolder.Context.Load($SubFolders)
$SourceFolder.Context.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=$TargetFolder.Context.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$TargetFolder.Context.load($Folder)
$TargetFolder.Context.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$TargetFolderURL
$Folder=$TargetFolder.Context.web.Folders.Add($TargetFolderURL)
$TargetFolder.Context.Load($Folder)
$TargetFolder.Context.ExecuteQuery()
Write-host "Folder Added:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively
Copy-Files -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SourceSiteURL="https://crescent.sharePoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/ops"
$SourceLibraryName="Project Documents"
$TargetLibraryName="Documents"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the source library and Target Libraries
$SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)
$SourceCtx.Load($SourceLibrary)
$SourceCtx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)
$TargetCtx.Load($TargetLibrary)
$TargetCtx.Load($TargetLibrary.RootFolder)
$TargetCtx.ExecuteQuery()
#Call the function
Copy-Files -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
Dosyaları kopyalamanın sürüm geçmişini korumayacağını belirtmekte fayda var! Sürüm oluşturmayı korumak istiyorsanız kopyalamak yerine “Taşı”yı kullanmanız gerekir.
Tüm Dosyaları Meta Veri değerleriyle birlikte Bir Kitaplıktan Başka Bir Kitaplığa Kopyalayın:
Bu komut dosyası, farklı site koleksiyonlarındaki belge kitaplıkları arasındaki tüm dosya ve klasörleri kopyalayarak şu meta veri alanı değerlerini korur: Oluşturan, Değiştiren, Oluşturan, Değiştiren Tarihi, vb.
#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 Copy-AllFilesWithMetadata
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$SourceFolder.Context.Load($SourceFilesColl)
$SourceFolder.Context.ExecuteQuery()
#Iterate through each file and copy
Foreach($SourceFile in $SourceFilesColl)
{
#Get the source file
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)
#Copy File to the Target location
$TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)
#Copy Metadata field values
$SourceListItem = $SourceFile.ListItemAllFields
$SourceFolder.Context.Load($SourceListItem)
$SourceFolder.Context.ExecuteQuery()
#Get the new file created
$TargetFile = $TargetFolder.Context.Web.GetFileByServerRelativeUrl($TargetFileURL)
$TargetListItem = $TargetFile.ListItemAllFields
#Set Metadata values from the source
$Author =$TargetFolder.Context.web.EnsureUser($SourceListItem["Author"].Email)
$TargetListItem["Author"] = $Author
$Editor =$TargetFolder.Context.web.EnsureUser($SourceListItem["Editor"].Email)
$TargetListItem["Editor"] = $Editor
$TargetListItem["Created"] = $SourceListItem["Created"]
$TargetListItem["Modified"] = $SourceListItem["Modified"]
$TargetListItem.Update()
$TargetFolder.Context.ExecuteQuery()
Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$SourceFolder.Context.Load($SubFolders)
$SourceFolder.Context.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=$TargetFolder.Context.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$TargetFolder.Context.load($Folder)
$TargetFolder.Context.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$TargetFolderURL
$Folder=$TargetFolder.Context.web.Folders.Add($TargetFolderURL)
$TargetFolder.Context.Load($Folder)
$TargetFolder.Context.ExecuteQuery()
Write-host "Folder Added:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively
Copy-AllFilesWithMetadata -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SourceSiteURL="https://crescent.sharepoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/Ops"
$SourceLibraryName="Project Documents"
$TargetLibraryName="Documents"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the source library and Target Libraries
$SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)
$SourceCtx.Load($SourceLibrary)
$SourceCtx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)
$TargetCtx.Load($TargetLibrary)
$TargetCtx.Load($TargetLibrary.RootFolder)
$TargetCtx.ExecuteQuery()
#Call the function
Copy-AllFilesWithMetadata -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
Dosyaları ve Klasörleri Site Koleksiyonları Arasında Kopyalamak için PnP PowerShell
Bu komut dosyası, PnP PowerShell cmdlet'i Copy-PnPFile'ı kullanarak belge kitaplığı klasörleri arasındaki dosya ve klasörleri bir site koleksiyonundan diğerine kopyalar. $SiteURL'yi kaynak sitenin URL'siyle, $TargetFolderURL'yi klasör hedef sitesinin sunucuya göre URL'siyle ve $SourceFolderURL'yi kopyalamak istediğiniz klasörün sunucuya göre URL'siyle değiştirdiğinizden emin olun.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$SourceFolderURL = "/sites/marketing/Branding/Logos"
$TargetFolderURL = "/sites/Purchase/Shared Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Copy All Files and Folders between source and target folders
Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force -OverwriteIfAlreadyExists
Bu komut dosyası, "Logolar" klasörünü Pazarlama sitesinin "Marka Oluşturma" kitaplığından Satın Almalar sitesinin "Paylaşılan Belgeler" kitaplığına kopyalar. Hedef sitedeki mevcut herhangi bir klasörü de belirtebilirsiniz.

