Wednesday, December 26, 2012

Copy SharePoint List or Library between Sites with PowerShell

In a migration project, we had to move few lists and libraries to different site collection. In SharePoint 2010, using granular export, we can export SharePoint lists and libraries from one site to another with SharePoint Central Administration web interface.
copy sharepoint 2010 list between sites
Once exported we've to use Import-SPSite PowerShell cmdlet to restore the list. Lists can be copied to different SharePoint site as well.
copy sharepoint list another site
Exporting from Central Admin UI and importing it again isn’t that much great time saver, So lets turn our attention to PowerShell to copy SharePoint 2010 list between sites. Here is my PowerShell script to Copy a single List or Library to another SharePoint Site:

Export Import a Library using PowerShell:

Export-SPWeb "http://your-site-url" -itemurl "/document" -path "c:\documents.cmp" -IncludeVersions All

Import-SPWeb "http://your-new-site-url/" -Path "c:\documents.cmp"

PowerShell Script to Backup-Restore (Or Export-Import) SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to copy List or Library from One SharePoint site to another 
Function CopyList([string]$SourceWebURL, [string]$TargetWebURL, [string]$ListName, [string]$BackupPath)
 {
    #Get the Source List
    $SourceList =(Get-SPWeb $SourceWebURL).Lists[$ListName]

    #Export the List from Source web
    Export-SPweb $SourceWebURL -ItemUrl $SourceList.DefaultViewUrl -IncludeUserSecurity -IncludeVersions All -path ($BackupPath + $ListName + ".cmp") -nologfile

   #Import the List to Target Web
    import-spweb $TargetWebURL -IncludeUserSecurity -path ($BackupPath + $ListName + ".cmp") -nologfile
 }
#Call the function
CopyList "http://sharepoint.crescent.com" "http://sharepoint.crescent.com/teams/marketing" "Banner" "C:\Temp\"

When I had a bunch of Lists to move from one site to another, I Just placed them in an array and called the function like this:

$Lists = @("Project Docs", "Project Metrics", "Milestones")

 foreach($List in $Lists)
 {
   #Call the function to copy
   CopyList "http://sharepoint.crescent.com" "http://sharepoint.crescent.com/teams/marketing" $List "C:\Temp\"
 }
 
 Write-Host "Completed!"

In an another situation, I had to copy of all Lists and libraries from a one Site Collection to another Site Collection. To export a single list use:
Export-SPweb "https://portal.crescent.com/News" -ItemUrl "Lists/Org News" -IncludeUserSecurity -IncludeVersions All -path "C:\Reports\News.cmp" -nologfile

PowerShell Script to Copy All Lists from One site to Another
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to copy List or Library from One SharePoint site to another 
Function CopyList([string]$SourceWebURL, [string]$TargetWebURL, [string]$ListName, [string]$BackupPath)
 {
    #Get the Source List
 $SourceList =(Get-SPWeb $SourceWebURL).Lists[$ListName]

   #Export the List from Source web
    Export-SPweb $SourceWebURL -ItemUrl $SourceList.DefaultViewUrl -IncludeUserSecurity -IncludeVersions All -path ($BackupPath + $ListName + ".cmp") -nologfile -Force

   #Import the List to Target Web
    import-spweb $TargetWebURL -IncludeUserSecurity -path ($BackupPath + $ListName + ".cmp") -nologfile -UpdateVersions Overwrite
 }
  
#Get All List Names
$Lists = @($(Get-SPWeb $SourceWebURL).lists)

 foreach($List in $Lists)
 {
  #Leave the Hidden Lists and exclude certain Libraries
    if($List.Hidden -eq $false -and $List.Title -ne "Style Library" -and $List.Title -ne "Site Pages")
 {
     #Call the function to copy
     CopyList "http://sharepoint.crescent.com" "http://sharepoint.crescent.com/HR/" $List.Title "C:\Temp\"
    }
 }
Write-Host "Completed Copying Lists!"
These scripts copies SharePoint list to another site including security and versions. Saving list as template with content, Export to spreadsheet are the OOTB methods for copying lists between site collections.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

3 comments :

  1. Nice article. FYI -The initial CopyList function script has the export command commented out.

    ReplyDelete
  2. Great Script – thanks a lot!

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...