Thursday, November 2, 2023

Copy a List in SharePoint Online Using PowerShell

Copy a List in SharePoint Online Using PowerShell

# 1. Copy with in site
$SiteURL = "https://SiteURL/sites/Retail/"
$SourceListName = "MyDocuments"
$DestinationListName = "MyDocuments_New"
 
Try {
  Connect-PnPOnline -Url $SiteURL -Interactive
  Copy-PnPList -Identity $SourceListName -Title $DestinationListName
}
Catch {
  write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
 
 
# 2. Copy to another site with in Site Collection
$SiteURL = "https://SourceSiteUrl/sites/Retail/"
$SourceListName = "MyDocuments"
 
$DestinationSiteURL = "https://DestinationSiteURL/sites/Sales/"
$DestinationListName = "MyDocuments_New"
 
Try {
  Connect-PnPOnline -Url $SiteURL -Interactive
  Copy-PnPList -Identity $SourceListName -Title $DestinationListName -DestinationWebUrl $DestinationSiteURL
}
Catch {
  write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
 
 
# 3. Copy to another Site Collection
$SourceSiteURL = "https://SourceSite/sites/one"
$TargetSiteURL = "https://TargetSite/sites/two"
$ListName = "TestList"
$TemplateFile = "C:\\Template.xml"
 
Connect-PnPOnline -Url $SourceSiteURL -Interactive
Get-PnPSiteTemplate -Out $TemplateFile -ListsToExtract $ListName -Handlers Lists
Add-PnPDataRowsToSiteTemplate -Path $TemplateFile -List $ListName
Connect-PnPOnline -Url $TargetSiteURL -Interactive
Invoke-PnPSiteTemplate -Path $TemplateFile

No comments:

Post a Comment

Featured Post

Azure OpenAI Chat in C# and Python

Azure OpenAI Chat in C#: // Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5   using System; u...

Popular posts