Wednesday, October 4, 2017

Move/Copy subsite to another site collection using PowerShell

Using SharePoint Server 2010;


Requirement:
We have a requirement to migrate SharePoint 2010 to SharePoint 2016. The existing SharePoint server 2010 have a subsite which has more subsites on it and the size is huge. We want to copy or move this subsite to a new site collection before we migrate the entire content to SP2016.

Note: Make sure you backup the existing content database and delete the subsite manually after verify the copy.


Solution:
Following powershell script is used to copy the subsite to new site collection which:
  1. Create a new content database
  2. Create a new site collection and attach to the new content database
  3. Export the subsite
  4. Import the subsite to the new site collection


PowerShell Script:

param
(
 $webApp = "http://sp2010-prasath:3333", #Web Application  URL
 $DBServer = "sp2010-prasath", #Database Server to create new content DB
 $DBName = "WSS_Content_Projects", #Content DB Name
 $subSiteURL = "http://sp2010-prasath:3333/Projects/", #Subsite URL
 $backupPath = "C:\\Sub Site Backup\\SandboxSubsSite.cmp", #Backup file
 $newSCURL = "http://sp2010-prasath:3333/sites/projects", #New site collection URL
 $SCOwner = "sp2010-prasath\spadmin", #Site Collection Administrator Account
 $errMessage = ""
)


Try
{
#Create new content database
$errMessage = "creating new Content Database"
$scGUID = New-SPContentDatabase $DBName -DatabaseServer $DBServer -WebApplication $webApp -ErrorAction Stop
Write-host "Content Database has been created successfully!"


#Create new site collection in the new content database
$errMessage = "creating new Site Collection"
$template = Get-SPWebTemplate "STS#0"  -ErrorAction Stop
$newSite = New-SPSite -Url $newSCURL -OwnerAlias $SCOwner -Template $template -ContentDatabase $scGUID.Id -ErrorAction Stop
Write-host "Site Collection has been created successfully!"


#Export/Backup existing subsite
$errMessage = "exporting subsite"
$export = Export-SPWeb -Identity $subSiteURL -Path $backupPath -IncludeUserSecurity -IncludeVersions all -HaltOnError -Force -ErrorAction Stop
Write-host "Subsite export successful"


#Import subsite to new site collection
$errMessage = "importing subsite to new Site Collection"
Import-SPWeb $newSCURL -Path $backupPath -ErrorAction Stop
Write-host "Import successful"
}
Catch
{
Write-host "Error occurred while" $errMessage
Write-host "Error Message:" $_.Exception.Message
}


Run the script in PowerShell

subsite to site collection

No comments:

Post a Comment