SharePoint Online: Copy Folder Structure between Document Libraries using PowerShell

Requirement: Copy Folder Structure between document libraries in SharePoint Online
Copy Folder Structure between SharePoint Online Document Libraries using PowerShell

PowerShell to Copy Folder Structure between SharePoint Online Document Libraries:
Ever wanted to clone folder, sub-folder structure between two SharePoint Online document libraries? Well, here is the PowerShell script to copy folder structure from one document library to another.
#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
This script copies all folders and sub-folders from source library to the target. If you want to copy folders along with files, use: SharePoint Online: Copy Files between Document Libraries using PowerShell

2 comments:

  1. I am having this error:
    Error Copying File! Exception calling "ExecuteQuery" with arguments "0": "Remote server error: (401) Unauthorized."

    ReplyDelete
    Replies
    1. Make sure the credentials you are entering has enough permissions in both source and target sites!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.