Forum Discussion

afierro97's avatar
afierro97
Copper Contributor
Oct 18, 2023
Solved

Problems moving files from site to site | PnP Sharepoint

Hello!

I have a file with all the paths to different files in different sites of my tenant that I need to move to a Specific Site in the same tenant, so I decided to use the PnP cmdlets for the job. This is giving me problems when I try to move a file to another Site.

For starters, if I Use these commands no file is found: 

 

 

Connect-PnPOnline -Url "https://tenant.sharepoint.com"
Get-PnPFile -Url "https://tenant.sharepoint.com/sites/Source Site/Documents/"

 

 

 To found a File I have to connect to the site like this:

 

 

Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"
Get-PnPFile -Url "https://tenant.sharepoint.com/sites/Source Site/Documents/myfile.txt"

 

 

 

this is giving me problems when I use the move command and I want to move a File from one site to another, because I'll only be able to be connected to 1 site at the same time.

The commands I use are these:

 

 

Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site/documents"

Move-PnPFile -SourceUrl  "https://tenant.sharepoint.com/sites/Source Site/documents/myfile.txt" -TargetUrl "https://tenant.sharepoint.com/sites/destination Site/documents"

 

 

This is the error I receive when I run those commands:

 

 

 

 

Line |
  25 |  Move-PnPFile -SourceUrl  $file -TargetUrl $trgtmove
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"odata.error":{"code":"-2147213276, Microsoft.SharePoint.Deployment.SPMigrationQosException","message":{"lang":"en-US","value":"Failed to verify the existence of source object at
     | 'https://tenant.sharepoint.com/sites/Source Site/Documents/myfile.txt' due to error 'The system cannot find the file specified. (Exception from
     | HRESULT: 0x80070002)'."}}}

 

 

 

I'm using MacOS with Visual Studio Code.

 

Can someone help with correcting the code or pointing me to the right tools for the job?

Also, is the first time I post something here so if I made a mistake, please let me know

 

 

 

  • afierro97's avatar
    afierro97
    Nov 01, 2023

    In case someone has the same problem as me, this is how i solved the problem.

    When connecting I connect to the source site

    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"

    then, when you use the move command use it like this:

    $SourceFileURL= "/sites/mySite/folder/file.txt" #Site Relative URL from the current site
    $TargetFileURL ="/sites/anotherSite/folder"
    Move-PnPFile -SourceUrl $SourceFileURL -TargetUrl $TargetFileURL

    if u use /sites/<the name of the site> you will be able to move the files the any site you want

     

     

3 Replies

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi afierro97,

    Thanks for your question.

    The problem you are facing is that the Copy-PnPFile cmdlet requires the source file to exist on the same site collection as the destination site collection. This is because the cmdlet uses the SharePoint migration infrastructure to copy the file.

    You can try to use this script adapting it to your needs:

     

    # Connect to the source site
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"
    
    # Get the PnPFile object for the file that you want to move
    $sourceFile = Get-PnPFile -Url "https://tenant.sharepoint.com/sites/Source Site/Documents/myfile.txt"
    
    # Disconnect from the source site
    Disconnect-PnPOnline
    
    # Connect to the destination site
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/destination Site"
    
    # Copy the file from the source site to the destination site
    Copy-PnPFile -SourceUrl $sourceFile.ServerRelativeUrl -TargetUrl "https://tenant.sharepoint.com/sites/destination Site/Documents"

     



    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic
    (LinkedIn)

    • afierro97's avatar
      afierro97
      Copper Contributor

      In case someone has the same problem as me, this is how i solved the problem.

      When connecting I connect to the source site

      Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"

      then, when you use the move command use it like this:

      $SourceFileURL= "/sites/mySite/folder/file.txt" #Site Relative URL from the current site
      $TargetFileURL ="/sites/anotherSite/folder"
      Move-PnPFile -SourceUrl $SourceFileURL -TargetUrl $TargetFileURL

      if u use /sites/<the name of the site> you will be able to move the files the any site you want

       

       

    • afierro97's avatar
      afierro97
      Copper Contributor

      Hi LeonPavesic! Thank you for the answer but that didn't work for me.

      After running the command and saving it to a variable It won't save nothing under the "ServerRelativeURL".

       

      Line |
        11 |   Copy-PnPFile -SourceUrl $sourceFile.ServerRelativeUrl -TargetUrl "ht …
           |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           | Cannot bind argument to parameter 'SourceUrl' because it is null.

       

      It's weird because if I print only what $sourceFile saved it shows the document I want. I'll investigate further on the problem of using that property

       

      Also I made a mistake posting the question the first time, I posted the copy command and wanted to show a move one but it gave me the exact same error. I've already fix the error in the post.

       

Resources