Wednesday, July 23, 2014

Export-Import All Lists and Libraries from One Site to Another using PowerShell

Requirement is to copy and paste all SharePoint lists and libraries from one site to another. While "Save List as a Template" is one approach, it doesn't work for larger lists and its time consuming. Using Central Administration export method also kills my time.

export import sharepoint list using powershell

Solution: We can copy and paste SharePoint lists using PowerShell script.

Export document library using PowerShell in SharePoint 2013:
Syntax:
Export-SPWeb <<Web-URL>> –Path <<Backup-Path>> -ItemURL <<Library-URL>> –IncludeUserSecurity  -IncludeVersions All

E.g.
Export-SPWeb http://sharepoint.crescent.com/Teams/2015-ESG/ –Path "D:\Documents.cmp" -ItemURL "Documents" –IncludeUserSecurity  -IncludeVersions All
 

Import document library in SharePoint 2013 using PowerShell:

Syntax:
Import-SPWeb <<web-URL>> -path <<Backup-file>> –force –IncludeUserSecurity -UpdateVersions Overwrite

E.g.
Import-SPWeb https://sharepoint.crescent.com/Teams/2016-ESG/" -Path "D:\Documents.cmp" –force –IncludeUserSecurity -UpdateVersions Overwrite
 

Export-Import all lists and libraries from one site to another site using PowerShell:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Custom PowerShell Function to Export All Lists and Libraries from a SharePoint site
Function Export-AllLists($WebURL, $ExportPath)
{
   #Get the source web
   $web = Get-SPWeb $WebURL

   #Check the Local Folder export Lists
    
   #Get all lists - Exclude System lists
   $ListCollection = $web.lists | Where-Object  { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.Author.LoginName -ne "SHAREPOINT\system") }

   #Iterate through each list and export
   foreach($list in $ListCollection)
    {
        Write-host "Exporting: " $list.Title
        #Remove : from List title - As we can't name a file with : symbol
        $ListTitle = $list.Title.Replace(":","")
        Export-SPWeb $WebURL -ItemUrl "/$($list.RootFolder.Url)" -IncludeUserSecurity -IncludeVersions All -path ($ExportPath + $ListTitle+ ".cmp") -nologfile
    } 
}

#Custom PowerShell Function to Export All Lists and Libraries from a SharePoint site
Function Import-AllLists($WebURL, $ImportPath)
{
   #Get the Target web
   $web = Get-SPWeb $WebURL

   #Check the Local Folder export Lists
    
   #Get all File Backups to Import
   $FilesCollection = Get-ChildItem $ImportPath

   #Iterate through each file and import
   foreach($File in $FilesCollection)
    {
        Write-host "Importing: " $File.Name
        Import-SPWeb $webURL -path $ImportPath$File -includeusersecurity -UpdateVersions Overwrite -nologfile
    } 
}

#Call the function to export
Export-AllLists "http://sales.crescent.com/" "D:\Backup\"
#To import, Use:
#Import-AllLists "http://marketing.crescent.com/" "D:\Backup\"
This script helped me to move all lists from a highly customized and corrupted site to a clean SharePoint site.Here is my another PowerShell script to copy-paste all lists and libraries between Sites: Copy SharePoint List or Library between Sites with PowerShell



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:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...