DevPoint

Microsoft .NET and SharePoint development

SharePoint solution deployment with PowerShell

As a developer I love to have everything automated and don’t like it too much to click around in SharePoint to deploy solutions, activate features or configure random stuff.

When developing SharePoint solutions we deliver WSP files and deployment instructions to the system engineers and they will deploy them to the different environments. But how great will it be to automate their work and deliver a single line deployment instruction like "Run this script!"? :-)

You can specify the name of WSP that’s currently installed and the name of the new WSP. This can be useful when you’re using version numbers in the WSP file names as you can see in the example script.

The script is fully configurable to your needs can perform the following actions:

  • Backup the currently installed solutions as WSP file
  • You can either choose to:
    • Uninstall (retract) and remove the solutions
    • Add the new solutions
    • Deploy the new solutions
  • Or:
    • Upgrade the installed solutions
  • Activate features on farm or web application level
  • Warmup the application pools after the deployment
  • Each step is logged in the PowerShell console

There’s also a method that will wait until the timer jobs (for retract, deployment and upgrade actions) are completed before processing the next action.

Each action can be used separately, just configure the boolean variables at the top of the script. Please note that you can’t use the upgrade actions when you also want to perform retract and/or remove actions.

#----------------------------------------------------------------------
# Set variables
#----------------------------------------------------------------------
$BackupSolutions = $true
$RemoveSolutions = $true
$AddSolutions = $true
$DeploySolutions = $true
$UpgradeSolutions = $false
$ActivateFeatures = $true
$WarmupWebApps = $true
$ConfirmDeleteSolution = $false
$ConfirmAddSolution = $false

$PortalURL = "http://DevPoint.vanbuiten.net"
$MySitesURL = "http://MySites.vanbuiten.net"

$WSPDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$BackupLocation = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
#----------------------------------------------------------------------
# Set the WSP names
#----------------------------------------------------------------------
$CoreWSPNameOld = "DevPoint.Intranet.Core.v1.wsp"
$CoreWSPNameNew = "DevPoint.Intranet.Core.v2.wsp"
$CoreWSP = Join-Path -Path $WSPDirectory -ChildPath $CoreWSPNameNew

$TimerJobsWSPNameOld = "DevPoint.Intranet.TimerJobs.v1.wsp"
$TimerJobsWSPNameNew = "DevPoint.Intranet.TimerJobs.v2.wsp"
$TimerJobsWSP = Join-Path -Path $WSPDirectory -ChildPath $TimerJobsWSPNameNew

Happy devving and deploying!

DevPoint-Solution-Deployment.ps1 (10.5KB)

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)