I need to migrate the documents of an existing library to a new library. The requirements of the migration is that I maintain versions and audit data (who/when created the document and last modified it).
The Export-SPWeb
and Import-SPWeb
cmdlets only seem to work on a list level or higher. This means that the old list gets created on import and includes custom views and what-not. But I want only the items to be migrated.
It is also my understanding that those cmdlets are ultimately using classes and methods in the Microsoft.SharePoint.Deployment
namespace.
Does anybody know how to do this?
Update
I have been researching the Content Migration API and here is what I have found.
Exporting Just List Items
$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[string]$sourceSiteUrl = "http://some.server.com"
[string]$sourceListName = "SomeList"
$fileLocation = $pwd
$site = New-Object Microsoft.SharePoint.SPSite($sourceSiteUrl)
$web = $site.OpenWeb()
$list = $web.Lists[$sourceListName]
$settings = New-Object Microsoft.SharePoint.Deployment.SPExportSettings
$settings.BaseFileName = "test.cmp"
$settings.CommandLineVerbose = $true
$settings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]::ExportAll
$settings.FileCompression = $true
$settings.FileMaxSize = [Int32]::MaxValue
$settings.IncludeSecurity = [Microsoft.SharePoint.Deployment.SPIncludeSecurity]::All
$settings.IncludeVersions = [Microsoft.SharePoint.Deployment.SPIncludeVersions]::CurrentVersion
$settings.LogFilePath = "$pwd\log.txt"
$settings.OverwriteExistingDataFile = $true
$settings.SiteUrl = $sourceSiteUrl
$settings.FileLocation = $fileLocation
ForEach ($i In $list.Items) {
$eo = New-Object Microsoft.SharePoint.Deployment.SPExportObject
$eo.Id = $i.UniqueId
$eo.Type = [Microsoft.SharePoint.Deployment.SPDeploymentObjectType]::ListItem
$settings.ExportObjects.Add($eo)
}
$settings.Validate()
$export = New-Object Microsoft.SharePoint.Deployment.SPExport($settings)
$export.Run()
This script will export all items (including documents) from the specified list. The key is adding each item to the ExportObjects
collection of the SPExportSettings
object.
Importing Just List Items
$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
[string]$destSiteUrl = "http://some.server.com" # or http://some.other.server.com
[string]$destListName = "SomeOtherList"
$fileLocation = $pwd
$site = New-Object Microsoft.SharePoint.SPSite($destSiteUrl)
$web = $site.OpenWeb()
$list = $web.Lists[$destListName]
$settings = New-Object Microsoft.SharePoint.Deployment.SPImportSettings
$settings.BaseFileName = "test.cmp"
$settings.CommandLineVerbose = $true
$settings.FileCompression = $true
$settings.LogFilePath = "$pwd\in-log.txt"
$settings.UserInfoDateTime = [Microsoft.SharePoint.Deployment.SPImportUserInfoDateTimeOption]::ImportAll
$settings.SiteUrl = $destSiteUrl
$settings.WebUrl = $destSiteUrl
$settings.FileLocation = $fileLocation
$settings.RetainObjectIdentity = $false
$settings.Validate()
$import = New-Object Microsoft.SharePoint.Deployment.SPImport($settings)
$oe = Register-ObjectEvent -InputObject $import -EventName Started -SourceIdentifier ImportStarted -Action {
$ro = $eventArgs.RootObjects
ForEach ($io in $ro) {
if ($io.Type -eq [Microsoft.SharePoint.Deployment.PDeploymentObjectType]::ListItem) {
$io.TargetParentUrl = $list.RootFolder.ServerRelativeUrl
}
}
}
$import.Run()
Get-EventSubscriber | Unregister-Event
This SHOULD perform the import on just the items but is not working correctly.
The problem seems to be around the Register-ObjectEvent
code. When an object gets imported into SharePoint, the object needs to have a parent url. Because I want to import into a list other than the source list, I need to re-parent the list items. According to the document, I should be using the Started
event of the SPImport
object to find the "orphaned" objects and point them to the desired list. For some reason, the Started
event is not being raised or if it is, PS is not executing my handler code.
Export-SPWeb
, extract the .cmp-file (7zip works), then add the .dat-files to wherever you want (their metadata is stored in Metadata.xmlExport-csv
the item data and save the files, although the cmp-file is kind of this, as it has all the data