DevPoint

Microsoft .NET and SharePoint development

SharePoint site archiving with content deployment in PowerShell

At some day one of our customers wanted to have a automated task to archive sites from one site collection to antoher. Because of the 100GB limit for a SharePoint site collection it was needed to move sites from the working site collection to an archive site collection. Also the performance of the search was an issue on the main site collection. It was not the intention to create a new solution and the request was to create a PowerShell script. As there was no development environment available this seemed to be the right way to go.

I started to create two functions, one to export the SharePoint site to an backup file and another function to import the backup file to the new location. It was needed to include all  the content, versions and the security of the site in the export file.

#--------------------------------------------------------------
# Export an existing site to an archive
#--------------------------------------------------------------
function ExportSite([string] $siteUrl, [string]$destination, [string]$fileName)
{
    try
    {
	    [Microsoft.SharePoint.Deployment.SPExportSettings]$exportSettings = New-Object Microsoft.SharePoint.Deployment.SPExportSettings
    
        $exportSettings.AutoGenerateDataFileName = $false;
        $exportSettings.BaseFileName = $fileName;
        $exportSettings.OverwriteExistingDataFile = $true;
        $exportSettings.FileLocation = $destination;
        $exportSettings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]::ExportAll
        $exportSettings.SiteUrl = $siteURL;
        $exportSettings.IncludeSecurity = [Microsoft.SharePoint.Deployment.SPIncludeSecurity]::All
        $exportSettings.IncludeVersions = [Microsoft.SharePoint.Deployment.SPIncludeVersions]::All

        $exportWeb = Get-SPWeb $siteUrl
		
        [Microsoft.SharePoint.Deployment.SPExportObject]$expObj = New-Object Microsoft.SharePoint.Deployment.SPExportObject
        $expObj.IncludeDescendants = [Microsoft.SharePoint.Deployment.SPIncludeDescendants]::Content
        $expObj.Id = $exportWeb.ID;
        $expObj.Type = [Microsoft.SharePoint.Deployment.SPDeploymentObjectType]::Web
        $exportSettings.ExportObjects.Add($expObj);
		
		[Microsoft.SharePoint.Deployment.SPExport]$export = New-Object Microsoft.SharePoint.Deployment.SPExport($exportSettings)
        $export.Run()
    }
    catch [Exception]
    {
        Write-Host -ForegroundColor Red "Error while creating backup file for site: $siteURL"
		Write-Host -ForegroundColor Red $_.Exception.Message
        return $null
    }

    return $exportSettings.FileLocation + "\\" + $exportSettings.BaseFileName;
}
#--------------------------------------------------------------
# Import a saved backup file
#--------------------------------------------------------------
function ImportSite([string] $siteUrl, [string]$destination, [string]$fileName)
{
    try 
    {
        [Microsoft.SharePoint.Deployment.SPImportSettings]$importSettings = New-Object Microsoft.SharePoint.Deployment.SPImportSettings

        $importSettings.FileLocation = $destination;
        $importSettings.BaseFileName = $fileName
        $importSettings.IncludeSecurity = [Microsoft.SharePoint.Deployment.SPIncludeSecurity]::All
        $importSettings.UpdateVersions = [Microsoft.SharePoint.Deployment.SPUpdateVersions]::Append
        $importSettings.RetainObjectIdentity = $false
        $importSettings.ActivateSolutions = $true
        $importSettings.UserInfoDateTime = [Microsoft.SharePoint.Deployment.SPImportUserInfoDateTimeOption]::ImportAll
        $importSettings.SiteUrl = $siteURL
        $importSettings.WebUrl = $siteURL
        
        [Microsoft.SharePoint.Deployment.SPImport]$import = New-Object Microsoft.SharePoint.Deployment.SPImport($importSettings)
        $import.Run();
    }
    catch [Exception]
    {
        Write-Host -ForegroundColor Red "Error while importing the backup file to site: $siteURL"
		Write-Host -ForegroundColor Red $_.Exception.Message
		
        return $false
    }

    return $true
}


To run this script we need to set up some variables and we're good to go:

$DeleteOriginalSite = $true
$PortalUrl = "http://sharepoint/site-to-move"
$ArchiveUrl = "http://sharepoint/sites/archive"
$ExportPath = [System.IO.Path]::GetTempPath()
$ExportFileName = "SP-Site-Archive.bak"

Write-Host "Creating backup file from site '$PortalUrl' to '$ExportPath'"

$exportResult = ExportSite $PortalUrl $ExportPath $ExportFileName

if ([string]::IsNullOrEmpty($exportResult) -eq $false)
{
	Write-Host "Backup is complete, moving site to '$ArchiveUrl'"
			
	$importResult = ImportSite $ArchiveUrl $ExportPath $ExportFileName
			
	if ($importResult -eq $true -and $DeleteOriginalSite -eq $true)
	{
		try
		{
			$originalWeb = Get-SPWeb $PortalUrl -AssignmentCollection $SPAssigment -ErrorAction Stop
        	$originalWeb.Delete()
        	$originalWeb.Dispose()
			Write-Host -ForegroundColor Green "Original site has been deleted on '$PortalUrl'"
		}
		catch [Exception]
		{
			Write-Host -ForegroundColor Red "Error while deleting original site on '$PortalUrl'"
			Write-Host -ForegroundColor Red $_.Exception.Message
		}
	}
}


The complete script can be downloaded from here:

SP-Archiving-ContentDeployment.ps1 (10.2KB)