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 }