3

I would like to know how to export a document library (SharePoint 2010) using PowerShell and also saving metadata about content. I need to repopulate the document library with items coming from itself. In other words, I want to separate list items of the list structure.

The procedure should follows the steps below:

  1. Export content of the document library (documents and associated metadata)
  2. Delete the document library
  3. Recreate the same document library (empty)
  4. Populate the document library with exported data

3 Answers 3

2

Export:

Export-SPWeb -Identity http://sharepointsite/sitecollection/ -itemurl "/site collection/library2" -path "c:\sharepoint\library2.cmp" -IncludeVersions All

Import:

Import-SPWeb -Identity  http://sharepointsite/sitecollection/  -Path "c:\sharepoint\library2.cmp" -Force

-Path is the file where you want to save the exported list. There are other attributes also take a look here https://technet.microsoft.com/en-us/library/ee428293%28v=office.14%29.aspx

1
  • Thank you for your answer, it's working. However, this is not what I'm looking for. I've edited the question, please take a look. I would like to be able to modify some metadata before inserting items inside the list.
    – ameliapond
    Dec 28, 2015 at 12:38
1

Use the below links for the Export-SPWeb and Import-SPWeb commands.

If you need permissions and versions then use additional commands with parameters like IncludeSecurity and IncludeVersions.

Note: If you are doing this activity on a subsite then you need to specify the ItemUrl property as well.

Example if you need export from subsite xyz with library pilo:

Export-SPWeb -Identity "http://sitename/xyz" -Path "D:\Folder\export.cmp" -ItemUrl "xyz/lists/pilo" -IncludeSecurity -IncludeVersion All

Import-SPWeb -Identity "http://newsite/wer" -Path "D:\Folder\export.cmp"
3
  • This is the first link I visited but it is not clear. What is -Path <Path and file name> ? What file are we talking about exactly ? Could you please provide an example ?
    – ameliapond
    Dec 23, 2015 at 15:41
  • The link clearly states : Before you begin, you must create a folder on the local computer or the network in which to store the export file. So the path is ur file system folder where you need the export
    – Nikhil J
    Dec 23, 2015 at 16:33
  • You're right, but I did not read the part for exporting using central administration because I don't want to use this method. So I wasn't aware of the cmp stuf.
    – ameliapond
    Dec 23, 2015 at 17:04
0

I'm not 100% sure of what you're trying to do, but it's similar to something I was trying to do, so I'm posting this here anyway.

But first, a little background.

  • My source server is a SharePoint 2010 server, my target server is SharePoint 2019. This means that I ran the Source part of the script on PowerShell 2 and the Target part on PowerShell 5.
  • This script will fail if your library exceeds 524,288,000 bytes. This cannot be determined from the export file size, so be careful. The script may delete your library leaving you unable to import it, and basically screwed.
  • I ran this solution from an account with full administrative credentials in SharePoint.
  • I was unable to run $Site=Get-SPSite "http://<sharepoint site>"; $Site.RecalculateStorageMetrics(), so I was unable to calculate any sizes.
  • The solution is not perfect, but it worked for me. Hopefully you can adjust it to meet your needs.

Good luck and don't run this script blind!

#=============Source=============
Clear-Host
if ($Host.Version.Major -ne 2) { throw "Use PowerShell Version 2"; break }

$Installed=Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.PowerShell" }
if ($Installed -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" }

$VerbosePreference="Continue"
$SourceWebURL="http://<sharepoint site>"
$SourceLibrary="<library name>"
$Parts=3
$ExportFolder="<fully qualified path>"

#=============Don't modify below=============

if (!(Test-Path -Path $ExportFolder -PathType Container)) { throw "Bad Export Path" }
$SourceWeb=Get-SPWeb -Identity $SourceWebURL -Verbose:$false

if (!$ExportFolder.EndsWith("\")) { $ExportFolder+="\" }
if ($SourceWebURL.EndsWith("/")) { $SourceWebURL=$SourceWebURL.Substring(0,$SourceWebURL.Length-1) }

# Make sure template doesn't already exist
$ExportTemplateName=$SourceLibrary+"Export"
Write-Verbose "Looking for pre-existing template $ExportTemplateName"
$CustomListTemplate=$Sourceweb.Site.GetCustomListTemplates($SourceWeb) | Where-Object { $_.Name -eq $ExportTemplateName }
if ($CustomListTemplate -ne $null) {
  Write-Verbose "Deleting Template: $ExportTemplateName"
  $ListTemplates=$SourceWeb.Site.RootWeb.GetFolder("_catalogs/lt")
  $Template=$ListTemplates.Files | Where-Object { $_.Name -eq $CustomListTemplate.InternalName }
  $Template.Delete()
}
$SourceWeb.RecycleBin.DeleteAll()

# Save library as content Template
# To save it as an empty Template, change $true to $false
Write-Verbose "Saving $SourceLibrary as content template: $ExportTemplateName"
$SourceWeb.Lists[$SourceLibrary].SaveAsTemplate($ExportTemplateName, $ExportTemplateName, $ExportTemplateName, $true)

# Export content templates
# PowerShell 2 version
$WebRequest=New-Object System.Net.WebClient
$WebRequest.UseDefaultCredentials=$true
$CustomListTemplate=$Sourceweb.Site.GetCustomListTemplates($SourceWeb) | Where-Object { $_.Name -eq $ExportTemplateName }
Write-Verbose "Exporting Content Template: $ExportTemplateName"
$Data=$WebRequest.DownloadData("$SourceWebURL/_catalogs/lt/$($CustomListTemplate.InternalName)")
[System.IO.File]::WriteAllBytes($ExportFolder+$CustomListTemplate.InternalName,$Data)

# Delete Library
Write-Verbose "Deleting Library: $SourceLibrary"
$Library=$SourceWeb.Lists[$SourceLibrary]
$Library.Delete()
$SourceWeb.RecycleBin.DeleteAll()

# Delete Content Template
$CustomListTemplate=$Sourceweb.Site.GetCustomListTemplates($SourceWeb) | Where-Object { $_.Name -eq $ExportTemplateName }
if ($CustomListTemplate -ne $null) {
  Write-Verbose "Deleting content template: $ExportTemplateName"
  $ListTemplates=$SourceWeb.Site.RootWeb.GetFolder("_catalogs/lt")
  $Template=$ListTemplates.Files | Where-Object { $_.Name -eq $CustomListTemplate.InternalName }
  $Template.Delete()
}
$SourceWeb.RecycleBin.DeleteAll()

#=============Target=============
Clear-Host

$Installed=Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.PowerShell" }
if ($Installed -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" }

$VerbosePreference="Continue"
$TargetWebURL="http://<sharepoint site>"
$TargetLibrary="<library name>"
$ImportFolder="<fully qualified path>"

#=============Don't modify below=============

$TargetWeb=Get-SPWeb -Identity $TargetWebURL -Verbose:$false

if (!$ImportFolder.EndsWith("\")) { $ImportFolder+="\" }
if ($TargetWebURL.EndsWith("/")) { $TargetWebURL=$TargetWebURL.Substring(0,$TargetWebURL.Length-1) }

# Get templates to import, make sure they don't exist
$Files=Get-ChildItem -Path $ImportFolder
$Files=@($Files)

$ListTemplateFiles=$TargetWeb.Site.RootWeb.GetFolder("_catalogs/lt")
$TargetSite=$TargetWeb.Site.OpenWeb()
$ListTemplates=$TargetSite.GetFolder("List Template Gallery")
$Templates=$ListTemplates.Files
for ($i=0; $i -lt $Files.Count; $i++) {
  Write-Verbose "Looking for pre-existing template $($Files[$i].BaseName)"
  $CustomListTemplate=$TargetWeb.Site.GetCustomListTemplates($TargetWeb) | Where-Object { ($_.InternalName -eq $Files[$i].Name) -or ($_.Name -eq $Files[$i].BaseName) }
  $CustomListTemplate | ForEach-Object { Write-Verbose "Deleting Template: $($_.Name)"; $TemplateInternalName=$_.InternalName; $Template=$ListTemplateFiles.Files | Where-Object { $_.Name -eq $TemplateInternalName }; $Template.Delete() }
  $TargetWeb.RecycleBin.DeleteAll()
  Write-Verbose "Adding Template: $($Files[$i].BaseName)"
  $Template=Get-Item -Path $Files[$i].FullName
  $Templates.Add("_catalogs/lt/$($Files[$i].Name)",$Template.OpenRead(),$true) > $null
}

# Make sure target library doesn't exist
$Library=$TargetWeb.Lists[$TargetLibrary]
if ($Library -ne $null) {
  Write-Verbose "Deleting Library: $TargetLibrary"
  $Library.Delete()
}
$TargetWeb.RecycleBin.DeleteAll()

# Create library from content Template
$Template=$TargetWeb.Site.GetCustomListTemplates($TargetWeb) | Where-Object { $_.Name -eq ($TargetLibrary+"Export") }
Write-Verbose "Creating Library: $TargetLibrary"
$TargetWeb.Lists.Add($TargetLibrary,$TargetLibrary,$Template) > $null

# Delete content template
$ListTemplates=$TargetWeb.Site.RootWeb.GetFolder("_catalogs/lt")
$Template=$ListTemplates.Files | Where-Object { $_.Name -eq $Files[$i].Name }
Write-Verbose "Deleting Template: $($Template.Name)"
$Template.Delete()
$TargetWeb.RecycleBin.DeleteAll()

# The file may still in use for a bit, so we may need to wait for the system to release them
$Template=$null
#Write-Verbose "Waiting 120 seconds for release of open file handles"
#Start-Sleep -Seconds 120
$Files | ForEach-Object { Write-Verbose "Deleting $($_.Name)"; Remove-Item -Path $_.FullName }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.