Copy SharePoint List or Document Library from one site to another

Post date: Apr 17, 2015 6:4:22 PM

This can be achieved by using the Content Deployment API and Powershell. First we export the list, then we import the list to the new site.

Exporting a list or document library

The first function exports a list or document library from a SharePoint site into either a folder or compressed .cmp file on a network or local drive.

function Export-List

{

Param (

[parameter(Mandatory=$true)][string]$WebUrl,

[parameter(Mandatory=$true)][string]$ListName,

[parameter(Mandatory=$true)][string]$Path,

[parameter(Mandatory=$false)][switch]$ExcludeDependencies,

[parameter(Mandatory=$false)][switch]$HaltOnWarning,

[parameter(Mandatory=$false)][switch]$HaltOnNonfatalError,

[parameter(Mandatory=$false)][switch]$AutoGenerateDataFileName,

[parameter(Mandatory=$false)][switch]$TestRun,

[parameter(Mandatory=$false)][string]$IncludeSecurity,

[parameter(Mandatory=$false)][string]$IncludeVersions,

[parameter(Mandatory=$false)][int]$FileMaxSize,

[parameter(Mandatory=$false)][switch]$Overwrite,

[parameter(Mandatory=$false)][switch]$SuppressLog

)

#Load SharePoint cmdlets

$ver = $host | select version

if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Load assemblies (needed for SharePoint Server 2007)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Check parameters have the correct values

if (!$IncludeSecurity)

{

$IncludeSecurity = "None"

}

else

{

if (($IncludeSecurity -ne "All") `

-and ($IncludeSecurity -ne "WssOnly") `

-and ($IncludeSecurity -ne "None"))

{

Throw "The IncludeSecurity parameter must be set to All, WssOnly or None"

}

}

if (!$IncludeVersions)

{

$IncludeVersions = "LastMajor"

}

else

{

if (($IncludeVersions -ne "All") `

-and ($IncludeVersions -ne "CurrentVersion") `

-and ($IncludeVersions -ne "LastMajor") `

-and ($IncludeVersions -ne "LastMajorAndMinor"))

{

Throw "The IncludeVersions parameter must be set to All, CurrentVersion, LastMajorAndMinor or LastMajor"

}

}

if (!$FileMaxSize)

{

$FileMaxSize = 0

}

$site = New-Object Microsoft.SharePoint.SPSite($WebUrl)

$web = $site.OpenWeb()

$list = $web.Lists[$ListName]

[bool]$FileCompression = $false

#Set file paths for the export file and logs

[string]$exportPath = $Path.TrimEnd("\")

if ($exportPath.EndsWith(".cmp"))

{

$FileCompression = $true

$exportFile = $Path.Replace($Path.Remove($Path.LastIndexOf("\")+1),"")

$exportPath = $Path.Remove($Path.LastIndexOf("\"))

}

$logFilePath = $exportPath + "\exportlog.txt"

New-Item -Path $logFilePath -Type File -Force | Out-Null

Write-Host "Export log file created at" $logFilePath

$exportObject = New-Object Microsoft.SharePoint.Deployment.SPExportObject

$exportObject.Id = $list.ID

$exportObject.Type = [Microsoft.SharePoint.Deployment.SPDeploymentObjectType]::List

#Create the export settings from the parameters specified

$exportSettings = New-Object Microsoft.SharePoint.Deployment.SPExportSettings

$exportSettings.SiteUrl = $site.Url

$exportSettings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]::ExportAll

$exportSettings.FileLocation = $exportPath

$exportSettings.FileCompression = $FileCompression

if ($FileCompression) { $exportSettings.BaseFileName = $exportFile }

$exportSettings.ExcludeDependencies = $ExcludeDependencies

$exportSettings.OverwriteExistingDataFile = $Overwrite

$exportSettings.IncludeSecurity = $IncludeSecurity

$exportSettings.IncludeVersions = $IncludeVersions

$exportSettings.LogFilePath = $logFilePath

$exportSettings.HaltOnWarning = $HaltOnWarning

$exportSettings.HaltOnNonfatalError = $HaltOnNonfatalError

$exportSettings.AutoGenerateDataFileName = $AutoGenerateDataFileName

$exportSettings.TestRun = $TestRun

$exportSettings.FileMaxSize = $FileMaxSize

$exportSettings.ExportObjects.Add($exportObject)

#Write the export settings to a log file

Out-File -FilePath $logFilePath -InputObject $exportSettings -Append -Encoding utf8

#Run the export procedure

$export = New-Object Microsoft.SharePoint.Deployment.SPExport($exportSettings)

$export.Run()

#Load notepad showing the log file

if (!$SuppressLog) { notepad $logFilePath }

#Dispose of the web and site objects after use

$web.Dispose()

$site.Dispose()

}

A list of the parameters available are shown in the following table. Much of this text is a straight copy from an MSDN article.

Here are a few examples of how the Export-List function can be used. First, this command will export a “Team Contacts” list and all list items from the site http://portal/sites/sales to the folder C:\Export\TeamContacts on the server, overwriting any existing export in that folder:

Export-List -WebUrl “http://portal/sites/sales” -ListName "Team Contacts" -Path "C:\Export\TeamContacts" -Overwrite

This one will export a “Team Documents” library and all documents contained within it from the site http://portal/sites/sales to the file C:\Export\TeamDocuments.cmp, overwriting any existing .cmp file with the same name. This time, we are also going to export permissions set on the library by using the IncludeSecurity parameter:

Export-List -WebUrl “http://portal/sites/sales” -ListName "Team Documents" -Path "C:\Export\TeamDocuments.cmp" -Overwrite -IncludeSecurity All

Importing a list or document library

Adding your exported list or document library to another SharePoint site can be achieved using the Import-List function below.

function Import-List

{

Param (

[parameter(Mandatory=$true)][string]$WebUrl,

[parameter(Mandatory=$true)][string]$Path,

[parameter(Mandatory=$false)][switch]$HaltOnWarning,

[parameter(Mandatory=$false)][switch]$HaltOnNonfatalError,

[parameter(Mandatory=$false)][switch]$RetainObjectIdentity,

[parameter(Mandatory=$false)][string]$IncludeSecurity,

[parameter(Mandatory=$false)][string]$UpdateVersions,

[parameter(Mandatory=$false)][switch]$SuppressLog

)

#Load SharePoint cmdlets

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Load assemblies (needed for SharePoint Server 2007)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Check parameters have the correct values

if (!$IncludeSecurity)

{

$IncludeSecurity = "None"

}

else

{

if (($IncludeSecurity -ne "All") `

-and ($IncludeSecurity -ne "WssOnly") `

-and ($IncludeSecurity -ne "None"))

{

Throw "The IncludeSecurity parameter must be set to All, WssOnly or None"

}

}

if (!$UpdateVersions)

{

$UpdateVersions = "Overwrite"

}

else

{

if (($UpdateVersions -ne "Overwrite") `

-and ($UpdateVersions -ne "Append") `

-and ($UpdateVersions -ne "Ignore"))

{

Throw "The UpdateVersions parameter must be set to Overwrite, Append or Ignore"

}

}

$site = New-Object Microsoft.SharePoint.SPSite($WebUrl)

$web = $site.OpenWeb()

$importSettings = New-Object Microsoft.SharePoint.Deployment.SPImportSettings

#Set file paths for the import file and logs

$fileName = ""

if ($Path.EndsWith(".cmp"))

{

$fileName = $Path.Replace($Path.Remove($Path.LastIndexOf("\")+1),"")

$importPath = $Path.Remove($Path.LastIndexOf("\"))

$importSettings.FileCompression = $true

$importSettings.BaseFileName = $fileName

}

else

{

$importPath = $Path.TrimEnd("\")

$importSettings.FileCompression = $false

}

$logFilePath = $importPath + "\importlog.txt"

New-Item -Path $logFilePath -Type File -Force | Out-Null

Write-Host "Import log file created at" $logFilePath

#Create the import settings from the parameters specified

Write-Host "Configuring import settings"

$importSettings.SiteUrl = $site.Url

$importSettings.WebUrl = $web.Url

$importSettings.FileLocation = $importPath

$importSettings.IncludeSecurity = $IncludeSecurity

$importSettings.UpdateVersions = $UpdateVersions

$importSettings.LogFilePath = $logFilePath

$importSettings.HaltOnWarning = $HaltOnWarning

$importSettings.HaltOnNonfatalError = $HaltOnNonfatalError

$importSettings.RetainObjectIdentity = $RetainObjectIdentity

$importSettings.UserInfoDateTime = "ImportAll"

if (!$SuppressLog) { $importSettings }

#Write the import settings to a log file

Out-File -FilePath $logFilePath -InputObject $importSettings -Append -Encoding utf8

#Run the import procedure

Write-Host "Import running, please wait..."

$import = New-Object Microsoft.SharePoint.Deployment.SPImport($importSettings)

$import.Run()

Write-Host "Import from" $Path "complete"

#Load notepad showing the log file

if (!$SuppressLog) { notepad $logFilePath }

#Dispose of the web and site objects after use

$web.Dispose()

$site.Dispose()

}

The parameters available for importing lists and document libraries are shown in the table below:

Here are a few examples of how the Import-List function can be used. First, this command will import the “Team Contacts” list and all list items from the exported files in the folder C:\Export\TeamContacts to the site http://intranet/sites/sales:

Import-List -WebUrl “http://intranet/sites/sales” -Path "C:\Export\TeamContacts"

This one will import the “Team Documents” library and all documents contained within it from the file C:\Export\TeamDocuments.cmp to the site http://intranet/sites/sales. This time, we are also going to import permissions set on the library by using the IncludeSecurity parameter:

Import-List -WebUrl "http://intranet/sites/sales” -Path "C:\Export\TeamDocuments.cmp" -IncludeSecurity All