Overview
Many times, we arrive in a situation when we want to move the SharePoint Online List/Library from one site to another site either with data or without data. This article describes how to move the master list with data and transaction list without data from one site to another site. e.g., Movement from Development to Quality or from Quality to Production.
Maintain the SharePoint List and Library which need to be copied from one site to another site
To keep the thinks configurable and this solution will have two files.
- Lists.csv - which will contain the list details which need to be copied from one site to another site. Edit the `Lists.csv` file and add your list name and a 0 or 1 for if this is a lookup list.
- environments.json - Edit this file to maintain environment details.
Sample Lists.cs
Sample Environments.json
{
"DEV": "https://missiono365.sharepoint.com/sites/POC",
"QA": "https://missiono365.sharepoint.com/sites/SpaceGame",
"PROD": "https://missiono365.sharepoint.com/sites/NorthwestSales"
}
PowerShell Script
class CopySpoList {
[string]$SourceUrl;
[string]$DestinationUrl;
[string]$DestinationEnvironment;
[string]$SourceEnvironment;
[Object]$Environments;
[bool]$IsEnvironmentValid = $true;
CopySpoList() {
Write-Host 'Below is the list of valid environments' -ForegroundColor Green;
Write-Host ('DEV', 'QA', 'PROD') -Separator ', -> ' -ForegroundColor DarkGreen;
$this.Environments = Get-Content -Path '..\environments.json' | ConvertFrom-Json;
$this.SourceEnvironment = Read-Host 'Enter the source environment';
$this.ValidateEnvironment($this.SourceEnvironment.ToUpper(), 'Source')
$this.DestinationEnvironment = Read-Host 'Enter the destination environment';
$this.ValidateEnvironment($this.DestinationEnvironment.ToUpper(), 'Destination')
}
[void] ValidateEnvironment([string]$environmentName, [string]$environmentType) {
switch ($environmentName) {
'DEV' {
if ($environmentType -eq 'Source') {
$this.SourceUrl = $this.Environments.DEV;
} else {
$this.DestinationUrl = $this.Environments.DEV;
}
break;
}
'QA' {
if ($environmentType -eq 'Source') {
$this.SourceUrl = $this.Environments.QA;
} else {
$this.DestinationUrl = $this.Environments.QA;
}
break;
}
'PROD' {
if ($environmentType -eq 'Source') {
$this.SourceUrl = $this.Environments.PROD;
} else {
$this.DestinationUrl = $this.Environments.PROD;
}
break;
}
Default {
$this.IsEnvironmentValid = $false;
Write-Host 'Value does not fall in valid environment range' -ForegroundColor Red;
Write-Host ('DEV', 'QA', 'PROD') -Separator ', -> ' -ForegroundColor Red
}
}
}
[void] MoveSpoList() {
# looping through the lists
$csvInput = Import-Csv -Path '.\Lists.csv'
foreach ($row in $csvInput) {
try {
# Connects SharePoint Online
Connect-PnPOnline -Url $this.SourceUrl.Trim() -Interactive
Get-PnPWeb
$ListName = $row.ListName.Trim();
$IsLookUpList = $row.IsLookUpList.Trim();
Write-Host $ListName
Write-Host $IsLookUpList
#Getting List Template Name
$timestamp = Get-Date -Format FileDateTimeUniversal
$tempFile = './SourceListBackup/' + $ListName + $timestamp + '.xml';
Get-PnPSiteTemplate -Handlers Lists -ListsToExtract $ListName -Out $tempFile
#Adding Rows to List Template
if ($IsLookUpList -eq 1) {
Add-PnPDataRowsToSiteTemplate -Path $tempFile -List $ListName -Query '<View></View>'
}
#Applying Template to Destination Site
Connect-PnPOnline -Url $this.DestinationUrl.Trim() -Interactive
Get-PnPWeb
Invoke-PnPSiteTemplate -Path $tempFile
} catch {
Write-Host -ForegroundColor Red 'Error ', ':', $Error[0].ToString();
Start-Sleep 10
}
}
}
}
[CopySpoList]$copySpList = [CopySpoList]::new();
if ($copySpList.IsEnvironmentValid) {
$copySpList.MoveSpoList();
}
Output
Summary
PnP PowerShell is a preferred way to go with modern SharePoint. It helps to easily move the SharePoint List from one site to another site.
Reference
https://pnp.github.io/powershell/index.html