Using PNP to Copy files between SharePoint Sites with metadata

%3CLINGO-SUB%20id%3D%22lingo-sub-1668631%22%20slang%3D%22en-US%22%3EUsing%20PNP%20to%20Copy%20files%20between%20SharePoint%20Sites%20with%20metadata%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1668631%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20trying%20to%20copy%20files%20between%20SharePoint%20sites.%26nbsp%3B%20I%20can%20not%20find%20an%20example%20of%20using%20the%20powershell%20PNP%20commands%20in%20sequence%20to%20perform%20the%20actions.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20not%20certain%20if%20I%20should%20be%20using%20Get-PNPListItems%20first%20then%20trying%20to%20use%20Get-PNPFile%20then%20using%20copy-pnpfile.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20see%20examples%20of%20these%20commands%20working%20individually%20but%20I%20don't%20see%20examples%20of%20using%20them%20in%20a%20real%20script%20to%20migrate%20files.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I'm trying to copy files between SharePoint sites.  I can not find an example of using the powershell PNP commands in sequence to perform the actions.  

 

I'm not certain if I should be using Get-PNPListItems first then trying to use Get-PNPFile then using copy-pnpfile.  

 

I see examples of these commands working individually but I don't see examples of using them in a real script to migrate files.

 

 

2 Replies

Hi @David Jenkins 

did you get anywhere with this...i have the same issue

I did actually get it to work. It's been a while so hopefully what I post makes sense.

I was copying list items from one sharepoint site to another.


First I got all items in the list.
#Define CAML Query
$qaDatabaseAll = Get-PnPListItem -List "QA Database" -Connection $sourceSite

Then do a foreach. like foreach($qaDBentry in $qaDatabaseAll){$qaDBentry}

Then the basic steps are to get the attachments. Sometimes theres more than one so foreach...
#Get Attachments for Entry
$attachments = ForEach-Object{Get-PnPProperty -ClientObject $qaDBentry -Property "AttachmentFiles" -Connection $sourceSite}

#Create new item
$newListItem = Add-PnPListItem -List "QA Database" -Values $qualityAlertValues -Connection $destinationSite -ContentType $qaDBentryContentType.Name

#Append Attachments
foreach($attachment in $attachments){
#Download the Attachment to Temp
# Fails because of versions?
#$File = Get-PnPFile -Url $attachment.ServerRelativeUrl -FileName $attachment.FileName -Path "D:\TEMP" -AsFile -force -Connection $sourceSite
#$env:TEMP

$fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($sourceCtx, $Attachment.ServerRelativeUrl) #
#$sourceCtx.ExecuteQuery()
#
$toPath = "D:\temp\" #
$fileName = $toPath + $attachment.FileName #
$fileStream = [System.IO.File]::Create($fileName) #
$fileInfo.Stream.CopyTo($fileStream); #
$sourceCtx.ExecuteQuery()
$fileStream.Close()

#Add Attachment to Target List Item
$FileStream = New-Object IO.FileStream(("D:\TEMP"+"\"+$attachment.FileName),[System.IO.FileMode]::Open)
$AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$AttachmentInfo.FileName = $attachment.FileName
$AttachmentInfo.ContentStream = $FileStream
$AttachFile = $newListItem.AttachmentFiles.add($AttachmentInfo)
$destinationCtx.ExecuteQuery()

#Delete the Temporary File
Remove-Item -Path "D:\TEMP\$($attachment.FileName)" -Force

}