Forum Discussion
Copy of a large number of folders from one sharepoint online site to another
- Feb 08, 2017
You can use the below script for copying. Attached the sample csv to be used. This script does not preserve metadata.
function Copy-sharepoint { Param( [Parameter(Mandatory=$True)] [String]$sharepointUrl, [Parameter(Mandatory=$True)] [String]$UserName, [Parameter(Mandatory=$True)] [String]$Password, [Parameter(Mandatory=$True)] [String]$Path, [Parameter(Mandatory=$True)] [System.Management.Automation.PSCredential]$cred ) #create secure password $sPassword = $Password | ConvertTo-SecureString -AsPlainText -Force $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") $csvs = Import-Csv $Path Foreach($csv in $csvs) { $Sourceurl = $csv.SourceUrl $souceFoldername= $csv.Foldername $targeturl = $csv.TargetUrl $TargetDocumentLib = $csv.targetlib Connect-PnPOnline -Url $Sourceurl -Credentials $cred $Files= Find-PnPFile -Folder $souceFoldername -Match * Disconnect-PnPOnline $Loginsource =$false $webClient = New-Object System.Net.WebClient $webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $sPassword) $webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f") $webclient.Proxy = $null Write-Host "Copying $($Files.count) files to $($targeturl)" foreach($file in $Files) { $pageUrl = $sharepointUrl+$file.ServerRelativeUrl $UniqueFileName = $file.Name $ByteArray=$webClient.DownloadData($pageUrl) $tfolderWithname= ($pageUrl -split $Sourceurl)[1] $tfolderwiths =($tfolderWithname -split $UniqueFileName)[0] $tfolderrmstr =$tfolderwiths.TrimStart("/") $tfolder =$tfolderrmstr.TrimEnd("/") $fstream = [System.IO.MemoryStream]($ByteArray) $tf=($tfolder -split "/") $test=$False If($tf.count -gt 1) { foreach($tf1 in $tf) { if($test -eq $true) { $tf2=$tf2+"/"+$tf1 } $test=$true } } Else { $tf2=$null } If($Loginsource -eq $false ) { Connect-PnPOnline -Url $targeturl -Credentials $cred $Loginsource =$true } $m=Get-PnPList -Identity $TargetDocumentLib $ctx=Get-PnPContext $mroot=$m.RootFolder $ctx.Load($mroot) $ctx.ExecuteQuery() $targetFolder=$mroot.Name+$tf2 $tf2=$null If($targetFolder -notlike "*Forms") { Add-PnPFile -FileName $UniqueFileName -Folder $targetFolder -Stream $fstream } } } } $cred=Get-Credential $Path = "C:\csv\copyfile.csv" $sharepointUrl = "https://tenantname.sharepoint.com" $UName=$cred.UserName.ToString() $Pass =$cred.GetNetworkCredential().Password Copy-sharepoint -UserName $UName -Password $Pass -cred $cred -sharepointUrl $sharepointUrl -Path $Path
I am trying Sharegate at the moment, but there seems to be a big limitation.
We have one Site with multiple folders. eg: customer1, customer2, customer3.
We have multiple Sites: Sitecustomer1, Sitecustomer2, Sitecustomer3.
When i edit the Excel file batch migrate in sharegate, i get a problem with the "DestinationPath"; i cant go up in the hierarchy with cd ../Sitecustomer1. I dont know if its even possible.
Chris
- Feb 08, 2017Mmm...you don't need to use the batch migration....simply copy objets from one site to the other and use the silent mode
- Christophe DAVIDFeb 08, 2017Copper Contributor
i have 1500 folders to copy to 1500 sites.
- Philip WorrellFeb 08, 2017Iron Contributor
Ok in that case look at the Sharegate powershell
https://support.share-gate.com/hc/en-us/categories/204661007-PowerShell
Really easy to set up a quick script which looks to a CSV
Pull the source and destination from the CSV and perform the copies.
Connect to the source and destination
Then perform the copy required copy cmdlet function and loop onto the next one.
You would have to do that with every tool or script you need.
Sharepoint sites are seperate entities and onwe does not know about another. So you have to connect to each site seperately. In the user intefaces you can only connect to one source and destination at a time.
- Guy JohnsonFeb 08, 2017Iron Contributor
Christophe DAVID agree totally with jcgonzalezmartin on this.
The straight copy works the faster in Sharegate. Have been using it for about 2 years now with no errors.