Hello @Christoph Maerz ,
Here is how you can Copy List or Document Library to Another Site using PnP PowerShell.
Step 1: Get the List Schema from Source Site
#Config Variables
$SiteURL = "https://test.sharepoint.com/sites/source/"
$ListName = "Project Tasks"
$TemplateFile = "C:\Temp\ListSchema.xml"
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get the List schema as Template and export to a File
$Templates = Get-PnPProvisioningTemplate -OutputInstance -Handlers Lists
$ListTemplate = $Templates.Lists | Where-Object { $_.Title -eq $ListName }
$Templates.Lists.Clear()
$Templates.Lists.Add($ListTemplate)
Save-PnPProvisioningTemplate -InputInstance $Templates -Out $TemplateFile
Step 2: Apply the Template in the Target Site
#Config Variables
$SiteURL = "https://test.sharepoint.com/sites/destination/"
$TemplateFile = "C:\Temp\ListSchema.xml"
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
Write-Host "Creating List from Template..."
Apply-PnPProvisioningTemplate -Path $TemplateFile
Thanks,
Echo Du
————————————————Updated Answer————————————————
Hi @Christoph Maerz ,
- Add-PnPDataRowsToProvisioningTemplate -Path template.pnp -List 'PnPTestList' -Query '' -IncludeSecurity
The fields to retrieve. If not specified all fields will be loaded in the returned list object. But some specific fields will not be loaded, such as ID.
So, I suggest you can enter the column name of the field you want.
For example:
- Add-PnPDataRowsToProvisioningTemplate -Path template.pnp -List 'PnPTestList' -Query '' -Fields 'Title','Choice' -IncludeSecurity
Complete code:
#Config Variables
$SiteURL = "https://test.sharepoint.com/sites/source/"
$ListName = "Alert"
$TemplateFile = "C:\Temp\template.pnp"
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get the List schema as Template and export to a File
$Templates = Get-PnPProvisioningTemplate -OutputInstance -Handlers Lists
$ListTemplate = $Templates.Lists | Where-Object { $_.Title -eq $ListName }
$Templates.Lists.Clear()
$Templates.Lists.Add($ListTemplate)
Save-PnPProvisioningTemplate -InputInstance $Templates -Out $TemplateFile
Add-PnPDataRowsToProvisioningTemplate -Path $TemplateFile -List 'Alert' -Query '' -Fields 'Title','Name','Name(hide)','Status'
#Config Variables
$SiteURL = "https://test.sharepoint.com/sites/destination/"
$TemplateFile = "C:\Temp\template.pnp"
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
Write-Host "Creating List from Template..."
Apply-PnPProvisioningTemplate -Path $TemplateFile
Thanks,
Echo Du
===============================
Updated Answer ===================================
Hi @Christoph Maerz ,
It is recommended that you consider the out-of-the-box function to achieve your design.
step1: Export template
- Sign in the source Task list as an admin and click on List Settings
- On the List Settings page, click on Save list as template
Note: Please select the Include Content checkbox to store items
- Go to Site settings page, click on List template under the Web Designer Galleries. Here you can see the template you just created.
- Then select this template, and then click on Download a Copy on the FILES tab
step2: Import template
- Sign in the destination site collection as an admin and go to Site settings page
- On the Site settings page, click on List template under the Web Designer Galleries.Then, click on Upload Document on the FILES tab to import the template you just downloaded
step3: Create a App
- Go to Site contents page(destination site collection), create a new App, you can retrieve the template you just imported and use it.
Note: The value of the Person or Group column can not copy
Thanks,
Echo Du
========================
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.