copy the document library with metadata from one to another site collection using PowerShell
1 Answer
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.Powershell
}
######################## Set Execution Path ################################################
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
################################# End of Set Execution Path #################################
$CurrentTime = Get-Date -Format yyyy-MM-dd_hh-mm
$res=Test-Path ".\log"
if($res -eq $false)
{
New-Item -ItemType Directory -Path ".\log"
}
$outputFilePath = ".\log\CopyLibrary_$CurrentTime.log"
############################# Move the library from source site collection to target Site Collection ########################
function MoveLibrary()
{
$srcSiteCollectionUrl=""
$srcSubsiteUrl =""
$srcLibraryTitle=""
$tgtSiteCollectionUrl=""
$tgtSubsiteUrl=""
$tgtLibraryTitle=""
# Get Source Site and verify exist or not
$sourceSite = Get-SPSite($srcSiteCollectionUrl) -ErrorAction Ignore
if($sourceSite)
{
#get source subsite
$sourceSubSite = $sourceSite.OpenWeb($srcSubsiteUrl)
if($sourceSubSite)
{
#Get document library from the subsite and verify for exist
$sourceLibrary = $sourceSubSite.Lists.TryGetList($srcLibraryTitle)
if($sourceLibrary)
{
$contentTpeColl = $sourceLibrary.ContentTypes
}
else
{
Add-Content $outputFilePath "Active Library Doesnot exist in current site , provide proper library title" -PassThru
}
}
else
{
Add-Content $outputFilePath "Please Provide proper Subsite Url" -PassThru
}
}
else
{
Add-Content $outputFilePath "Please Provide proper active Site collection Url" -PassThru
}
# Get the target Context,Subsite and library
$targetSite = Get-SPSite($tgtSiteCollectionUrl) -ErrorAction Ignore
if($targetSite)
{
$targetSubSite = $targetSite.OpenWeb($tgtSubsiteUrl)
if($targetSubSite)
{
if($sourceLibrary)
{
# Check and create target Library if doesnot exist
$targetLibrary = $targetSubSite.Lists.TryGetList($tgtLibraryTitle)
if(!$targetLibrary)
{
$targetSubSite.Lists.Add($srcLibraryTitle,$srcLibraryTitle,101)
$targetLibrary = $targetSubSite.Lists.TryGetList($tgtLibraryTitle)
}
$tSiteContentTypeColl = $targetSite.OpenWeb().AvailableContentTypes
$sLibraryContentTypeColl = $sourceLibrary.ContentTypes
if(!$targetLibrary.ContentTypesEnabled)
{
$targetLibrary.ContentTypesEnabled = $true
$targetLibrary.Update()
}
# Add source library Content types to target library
foreach($sContent in $sLibraryContentTypeColl)
{
$sId = $tSiteContentTypeColl.BestMatch($sContent.Id)
if(!$targetLibrary.ContentTypes[$sContent.Name])
{
$targetLibrary.ContentTypes.Add($tSiteContentTypeColl[$sId])
$targetLibrary.Update()
}
}
}
else
{
continue
}
}
}
$formsUrl = $sourceLibrary.RootFolder.Url + "/"+ "Forms"
$sLibUrlLength = $sourceLibrary.RootFolder.Url.length
$sourceFolderCollection = $sourceLibrary.Folders|where {$_.Url -ne $formsUrl}
#$tFolderUrl = $sourceLibrary.RootFolder.Url
try
{
foreach($sourceFolder in $sourceFolderCollection)
{
# append the compltre url of the library to create non existing folders
$tFolderUrl = $targetSubSite.Url + "/" + $targetLibrary.RootFolder.Url
$tLibraryUrl = $targetLibrary.RootFolder.Url
$sFolders = $sourceFolder.Url.substring($sLibUrlLength).Split('/')
$i=0
while($i -lt ($sFolders.count-1))
{
if($sFolders[$i])
{
$tFolderUrl = "$tFolderUrl/" + $sFolders[$i]
}
#Increment the I variable in order to move forward through the folder structure
$i++
}
#verify folder exist in target Library ,if not creat new folder
$tLibraryUrl = $targetLibrary.RootFolder.Url + $sourceFolder.Url.substring($sLibUrlLength)
if(!($targetLibrary.Folders | ? {$_.Url -eq $tLibraryUrl}))
{
$tFolder = $targetLibrary.Folders.Add($tFolderUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$sourceFolder.Name)
$tFolder.update()
$sFolderItem = $sourceFolder.Folder.Item
$tFolderItem = $tFolder.Folder.Item
$sFolderFields = $sFolderItem.Fields |?{!($_.sealed)}
# Update target File Metadata from source file metadata
foreach($sFolderField in $sFolderFields)
{
if($sFolderField.ReadOnlyField -ne $true -and $sFolderField.Hidden -ne $true)
{
$tFolderItem[$sFolderField.InternalName] = $sFolderItem[$sFolderField.InternalName]
}
}
$tFolderItem["ContentTypeId"] = $sFolderItem.Item["ContentTypeId"]
$tFolderItem.Update()
$tFolder.Update()
}
else
{
$tFolder = $targetLibrary.Folders | ? {$_.Url -eq $tLibraryUrl}
}
$sFileCollection = $sourceFolder.Folder.Files
foreach($sFile in $sFileCollection)
{
try
{
#Upload the file to target folder
$sBinary = $sFile.OpenBinary()
$tFile = $tFolder.Folder.Files.Add($sFile.Name , $sBinary , $true)
$sItem = $sFile.Item
$tItem = $tFile.Item
$sFileFields = $sItem.Fields |?{!($_.sealed)}
# Update target File Metadata from source file metadata
foreach($sField in $sFileFields)
{
if($sField.ReadOnlyField -ne $true -and $sField.Hidden -ne $true)
{
$tItem[$sField.InternalName] = $sItem[$sField.InternalName]
}
}
$tItem["ContentTypeId"] = $sFile.Item["ContentTypeId"]
$tItem.Update()
$tFile.Update()
}
catch [System.Exception]
{
Add-Content $outputFilePath $_.Exception.ToString() -PassThru
continue
}
}
}
}
catch [System.Exception]
{
Add-Content $outputFilePath $_.Exception.ToString() -PassThru
}
#$sourceFolderCollection|where {$_.Url -ne $formsUrl}
# $sourceFolderCollection
#CheckAndCreateFolderStructure
}
MoveLibrary