SharePoint Online: PowerShell kullanarak Tüm Belge Kitaplıklarını Başka Bir Siteye Kopyalayın


Gereksinim: Tüm Belge Kitaplıklarını SharePoint Online'daki başka bir Site koleksiyonuna kopyalayın.

Belge Kitaplıklarını Bir Siteden Başka Bir Site Koleksiyonuna Kopyalamak için PowerShell:

Tüm SharePoint Online belge kitaplıklarınızı bir siteden diğerine taşımak mı istiyorsunuz? Eğer öyleyse, hepsini kolayca kopyalamak için PowerShell'i kullanabilirsiniz. SharePoint Online'da belge kitaplığının tamamını bir siteden diğerine kopyalamak mümkündür. Mevcut bir kitaplığı yeni bir proje için başlangıç noktası olarak yeniden kullanmak istiyorsanız veya bir kitaplığı başka bir sitedeki iş arkadaşlarınızla paylaşmak istiyorsanız bu yararlı olabilir. Bu öğreticide, bir belge kitaplığının SharePoint Online'da başka bir siteye nasıl kopyalanacağını size göstereceğiz.

Benim durumumda, bir geçiş projesi sırasında, tüm belge kitaplıklarını bir site koleksiyonundan diğerine kopyalamam gerekiyor. Site koleksiyonları arasında tüm kitaplıkları (yalnızca gerçek içeriği olmayan kitaplığı) kopyalamak için kullandığım PowerShell betiği:


#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/Neoma"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/Neoma-Copy"

#Connect to the source Site
Connect-PnPOnline -URL $SourceSiteURL -Interactive 

#Get all document libraries
$SourceLibraries =  Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

#Connect to the destination site
Connect-PnPOnline -URL $DestinationSiteURL -Interactive

#Get All Lists in the Destination
$Libraries = Get-PnPList

ForEach($Library in $SourceLibraries)
{
    #Check if the library already exists in target
    If(!($Libraries.Title -contains $Library.Title))
    { 
        #Create a document library
        New-PnPList -Title $Library.Title -Template DocumentLibrary
        Write-host "Document Library '$($Library.Title)' created successfully!" -f Green
    }
    else
    {
        Write-host "Document Library '$($Library.Title)' exists already!" -f Yellow
    }
}

SharePoint Online: PowerShell, Kitaplıkları İçeriğiyle birlikte Siteler Arasında Kopyalayacak

Bu sefer tüm belge kitaplıklarını, belge kitaplıklarının içerikleriyle (Dosya, Klasör, Alt klasör vb.) birlikte başka bir site koleksiyonuna kopyalayalım:


#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/Branding"

#Connect to the source Site
$SourceConn = Connect-PnPOnline -URL $SourceSiteURL -Interactive -ReturnConnection
$Web = Get-PnPWeb -Connection $SourceConn

#Get all document libraries
$SourceLibraries =  Get-PnPList -Includes RootFolder -Connection $SourceConn | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

#Connect to the destination site
$DestinationConn = Connect-PnPOnline -URL $DestinationSiteURL -Interactive -ReturnConnection

#Get All Lists in the Destination site
$DestinationLibraries = Get-PnPList -Connection $DestinationConn

ForEach($SourceLibrary in $SourceLibraries)
{
    #Check if the library already exists in target
    If(!($DestinationLibraries.Title -contains $SourceLibrary.Title))
    { 
        #Create a document library
        $NewLibrary  = New-PnPList -Title $SourceLibrary.Title -Template DocumentLibrary -Connection $DestinationConn
        Write-host "Document Library '$($SourceLibrary.Title)' created successfully!" -f Green
    }
    else
    {
        Write-host "Document Library '$($SourceLibrary.Title)' already exists!" -f Yellow
    }

    #Get the Destination Library
    $DestinationLibrary = Get-PnPList $SourceLibrary.Title -Includes RootFolder -Connection $DestinationConn
    $SourceLibraryURL = $SourceLibrary.RootFolder.ServerRelativeUrl
    $DestinationLibraryURL = $DestinationLibrary.RootFolder.ServerRelativeUrl
    
    #Calculate Site Relative URL of the Folder
    If($Web.ServerRelativeURL -eq "/")
    {
	    $FolderSiteRelativeUrl = $SourceLibrary.RootFolder.ServerRelativeUrl
    }
    Else
    {      
	    $FolderSiteRelativeUrl = $SourceLibrary.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeURL,[string]::Empty)
    }

    #Get All Content from Source Library's Root Folder
    $RootFolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -Connection $SourceConn | Where {($_.Name -ne "Forms") -and (-Not($_.Name.StartsWith("_")))}
        
    #Copy Items to the Destination
    $RootFolderItems | ForEach-Object {
        $DestinationURL = $DestinationLibrary.RootFolder.ServerRelativeUrl
        Copy-PnPFile -SourceUrl $_.ServerRelativeUrl -TargetUrl $DestinationLibraryURL -Force -OverwriteIfAlreadyExists
        Write-host "`tCopied '$($_.ServerRelativeUrl)'" -f Green    
    }    
    Write-host "`tContent Copied from $SourceLibraryURL to  $DestinationLibraryURL Successfully!" -f Cyan
}

Yukarıdaki komut dosyası, basit belge kitaplıklarıyla mükemmel şekilde çalışır. Ancak belge kitaplığınıza özel meta veri sütunları eklenmişse ne olur? Belge kitaplıklarını özel ayarlarıyla birlikte kopyalamak isterseniz ne olur? İşte, SharePoint Online'daki belge kitaplığını kopyalamak için liste şablonu oluşturma ve liste örnekleri yöntemini kullanan PowerShell betiği!

PowerShell, Belge Kitaplıklarını SharePoint Online'daki Site Koleksiyonları Arasında Kopyalamak İçin

Bu PowerShell betiği, listeleri ve bunların ayarlarını ve meta veri sütunlarını hedef siteye kopyalar.

Bu betiği çalıştırmadan önce SharePoint Online'da özel betiği etkinleştirdiğinizden emin olun! Aksi halde “Erişim reddedildi” hatasıyla karşılaşırsınız. Bu eylemi gerçekleştirme veya bu kaynağa erişme izniniz yok." SharePoint Online'da Özel Komut Dosyası Nasıl Etkinleştirilir?

#Function to Copy All libraries from One Site to another
Function Copy-PnPAllLibraries
{
    param (
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$SourceSiteURL,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$DestinationSiteURL
    )
 
    Try {
        #Connect to the Source Site
        $SourceConn = Connect-PnPOnline -URL $SourceSiteURL -Interactive -ReturnConnection
        $SourceCtx = $SourceConn.Context
        $SourceRootWeb = $SourceCtx.Site.RootWeb
        $SourceCtx.Load($SourceRootWeb)
        $SourceCtx.ExecuteQuery()

        #Connect to the Destination Site
        $DestinationConn = Connect-PnPOnline -URL $DestinationSiteURL -Interactive -ReturnConnection
        $DestinationCtx = $DestinationConn.Context
        $DestinationRootWeb = $DestinationCtx.Site.RootWeb
        $DestinationCtx.Load($DestinationRootWeb)
        $DestinationCtx.ExecuteQuery()    
 
        #Exclude certain libraries
        $ExcludedLibraries =  @("Style Library","Preservation Hold Library", "Site Pages", "Site Assets","Form Templates", "Site Collection Images","Site Collection Documents")
    
        #Get Libraries from Source site - Skip hidden and certain libraries
        $SourceLibraries =  Get-PnPList -Includes RootFolder -Connection $SourceConn | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLibraries}
        
        #region PrepareTemplates
        $SourceListTemplates = $SourceCtx.Site.GetCustomListTemplates($SourceRootWeb)
        $SourceCtx.Load($SourceListTemplates)
        $SourceCtx.ExecuteQuery()
        $DestinationListTemplates = $DestinationCtx.Site.GetCustomListTemplates($DestinationRootWeb)
        $DestinationCtx.Load($DestinationListTemplates)
        $DestinationCtx.ExecuteQuery()

        #Remove Document Library Templates from source and destination sites
        ForEach($Library in $SourceLibraries)
        {
            $SourceListTemplate = $SourceListTemplates | Where {$_.Name -eq $Library.id.Guid}
            $SourceListTemplateURL = $SourceRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$Library.id.Guid+".stp"   
 
            #Remove the List template if exists in source   
            If($SourceListTemplate)
            {
                $SourceListTemplateFile = Get-PnPFile -Url $SourceListTemplateURL -Connection $SourceConn
                $SourceListTemplateFile.DeleteObject()
                $SourceCtx.ExecuteQuery()
            }
        }

        Write-host "Creating List Templates..." -f Yellow -NoNewline
        #Create Templates
        $SourceLibraries | ForEach-Object {
            $_.SaveAsTemplate($_.id.Guid, $_.id.Guid, [string]::Empty, $False)
            $SourceCtx.ExecuteQuery()
  
            #Copy List Template from source to the destination site
            $SourceListTemplateURL = $SourceRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$_.id.Guid+".stp"   
            Copy-PnPFile -SourceUrl $SourceListTemplateURL -TargetUrl ($DestinationRootWeb.ServerRelativeUrl+"/_catalogs/lt") -Force -OverwriteIfAlreadyExists
        }
        Write-host "Done!" -f Green
        Start-Sleep 5

        #Reload the List Templates in the Destination Site
        $DestinationListTemplates = $DestinationCtx.Site.GetCustomListTemplates($DestinationRootWeb)
        $DestinationCtx.Load($DestinationListTemplates)
        $DestinationCtx.ExecuteQuery()
        #endregion

        #Iterate through each library in the source
        ForEach($SourceLibrary in $SourceLibraries)
        {
            Write-host "Copying library:"$SourceLibrary.Title -f Magenta

            #Get the Template
            $DestinationListTemplate = $DestinationListTemplates | Where {$_.Name -eq $SourceLibrary.id.Guid}
 
            #Create the destination library from the list template, if it doesn't exist
            Write-host "Creating New Library in the Destination Site..." -f Yellow -NoNewline
            If(!(Get-PnPList -Identity $SourceLibrary.Title -Connection $DestinationConn))
            {
                #Create the destination library
                $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListCreation.Title = $SourceLibrary.Title
                $ListCreation.ListTemplate = $DestinationListTemplate
                $DestinationList = $DestinationCtx.Web.Lists.Add($ListCreation)
                $DestinationCtx.ExecuteQuery()
                Write-host "Library '$($SourceLibrary.Title)' created successfully!" -f Green
            }
            Else
            {
                Write-host "Library '$($SourceLibrary.Title)' already exists!" -f Yellow
            }

            Write-host "Copying Files and Folders from the Source to Destination Site..." -f Yellow    
            $DestinationLibrary = Get-PnPList $SourceLibrary.Title -Includes RootFolder -Connection $DestinationConn
            #Copy All Content from Source Library's Root Folder to the Destination Library
            If($SourceLibrary.ItemCount -gt 0)
            {
                #Get All Items from the Root Folder of the Library
                $global:counter = 0
                $ListItems = Get-PnPListItem -List $SourceLibrary.Title -Connection $SourceConn -PageSize 500 -Fields ID -ScriptBlock {Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
                    (($global:Counter / $SourceLibrary.ItemCount) * 100) -Activity "Getting Items from List" -Status "Getting Items $global:Counter of $($SourceLibrary.ItemCount)"}
                $RootFolderItems = $ListItems | Where { ($_.FieldValues.FileRef.Substring(0,$_.FieldValues.FileRef.LastIndexOf($_.FieldValues.FileLeafRef)-1)) -eq $SourceLibrary.RootFolder.ServerRelativeUrl}
                Write-Progress -Activity "Completed Getting Items from Library $($SourceLibrary.Title)" -Completed
        
                #Copy Items to the Destination
                $RootFolderItems | ForEach-Object {
                    $DestinationURL = $DestinationLibrary.RootFolder.ServerRelativeUrl
                    Copy-PnPFile -SourceUrl $_.FieldValues.FileRef -TargetUrl $DestinationLibrary.RootFolder.ServerRelativeUrl -Force -OverwriteIfAlreadyExists
                    Write-host "`tCopied $($_.FileSystemObjectType) '$($_.FieldValues.FileRef)' Successfully!" -f Green     
                }
            }
        }

        #Cleanup List Templates in source and destination sites
        ForEach($Library in $SourceLibraries)
        {
            $SourceListTemplate = $SourceListTemplates | Where {$_.Name -eq $Library.id.Guid}
            $SourceListTemplateURL = $SourceRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$Library.id.Guid+".stp"   
 
            #Remove the List template if exists in source   
            If($SourceListTemplate)
            {
                #Remove-PnPFile -ServerRelativeUrl $SourceListTemplateURL -Recycle -Force -Connection $SourceConn
                $SourceListTemplateFile = Get-PnPFile -Url $SourceListTemplateURL -Connection $SourceConn
                $SourceListTemplateFile.DeleteObject()
                $SourceCtx.ExecuteQuery()
            }
            #Remove the List template if exists in target 
            $DestinationListTemplate = $DestinationListTemplates | Where {$_.Name -eq $Library.id.Guid}
            $DestinationListTemplateURL = $DestinationRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$Library.id.Guid+".stp" 
            #Remove the List template if exists    
            If($DestinationListTemplate)
            {
                #Remove-PnPFile -ServerRelativeUrl $DestinationListTemplateURL -Recycle -Force -Connection $DestinationConn
                $DestinationListTemplate = Get-PnPFile -Url $DestinationListTemplateURL -Connection $DestinationConn
                $DestinationListTemplate.DeleteObject()
                $DestinationCtx.ExecuteQuery()        
            }
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/HR"

#Call the function to copy libraries to another site
Copy-PnPAllLibraries -SourceSiteURL $SourceSiteURL -DestinationSiteURL $DestinationSiteURL

Tek bir belge kitaplığını başka bir site koleksiyonuna kopyalamak için şunu kullanın: PowerShell kullanılarak bir belge kitaplığı SharePoint Online'da başka bir siteye nasıl kopyalanır?

İlgili makaleler