powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[FEATURE] - Copy-PnPFile from SPO to ODB

Open omarmallat opened this issue 2 years ago • 1 comments

Reporting an Issue or Missing Feature

Copy-PnPFile does not work if -SourceUrl is /sites/something/Shared Documents/ and -TargetUrl is /personal/someone_domain_com/Documents it works properly if both are /sites/... or both are /personal/...

Expected behavior

should be able to use it to copy items from sharepoint online to onedrive for business.

Actual behavior

The remote server returned an error: (404) Not Found.

Steps to reproduce behavior

Please include complete script or code samples in-line or linked from gists

What is the version of the Cmdlet module you are running?

(you can retrieve this by executing Get-Module -Name "PnP.PowerShell" -ListAvailable)

Which operating system/environment are you running PnP PowerShell on?

  • [ ] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

omarmallat avatar Mar 24 '22 07:03 omarmallat

@omarmallat - This is not a bug as such. There are some authentication challenges when copying from SPO to ODFB unfortunately. You can have a look at the discussion here if you want to try (although it's talking the opposite way ODFB to SPO) but you may try to adjust.

I'll classify as enhancement.

veronicageek avatar Mar 24 '22 09:03 veronicageek

hi @omarmallat , we have fixed this issue with the latest nightly builds.

You will need to provide a full destination URL for this to work. i.e , the -TargetUrl parameter value needs to be something like https://tenant-my.sharepoint.com/personal/..

The source URL value should be as it is.

gautamdsheth avatar Sep 11 '23 17:09 gautamdsheth

@gautamdsheth just wanted to add that your updates in the nightly build worked well for me as well. This is a hugely appreciated update as it helped me meet a need to copy missing files from OneDrive to SharePoint Online. Thank you!

jreinhardtproarch avatar Nov 01 '23 12:11 jreinhardtproarch

Hello, may I ask how you're connecting to 2 different sites and using this method? I have been unsuccessful in connecting to multiple sites, such as a ODFB and a SharePoint site in order to transfer files.

Beegeebus avatar Jan 22 '24 15:01 Beegeebus

I can confirm it's working now. My concept was to copy from SharePoint to OneDrive in 3 steps:

  1. Connect to destination OneDrive and make sure the container folder is created.
  2. Connect to the source SharePoint
  3. Copy a specific file/folder to the destination

I used the following code:

$DestinationConn = Connect-PnPOnline -URL $DestinationSiteURL -Interactive -ReturnConnection
$DestinationRelativePath = $OneDriveDefaultListName + "/" + $DestinationFolderPath
try {
      Add-PnPFolder -Connection $DestinationConn -Name $DestinationProjectName -Folder $DestinationRelativePath 
}
catch {
     Write-Host "The project's folder $($SourceSiteCode) already exists in the destination. No need to create it."
}
$SourceConn = Connect-PnPOnline -URL $SourceSiteURL -Interactive -ReturnConnection
try {
       Copy-PnPFile -Connection $SourceConn -SourceUrl $f.ServerRelativeUrl -TargetUrl $DestinationLibraryURL -Force -OverwriteIfAlreadyExists 
                
     }
catch {
      write-host -f Red "Error:" $_.Exception.Message
}

the code above was used with the following criteria:

  1. I had to include the parameter -Connection
  2. SourceUrl is only the relative URL, where it starts with /sites/SITENAME/Share Documents/PATH_TO_FOLDER_OR_FILE
  3. TargetUrl is the full path starting from https://TENANT-my.sharepoint.....

omarmallat avatar Jan 29 '24 08:01 omarmallat