Exporting SharePoint 2013 Search Customisations

​I had a client the other day who have a production enviorment and have just created a development enviorment.  This particular client have a LOT of search customisations and more than a handful of result types, result sources, etc.  Unfortunately when the content DB which was a copy of the prod enviorment was restored to the dev enviorment, it did not include any of these customisations.

With the help of a good colleague of mine we put together some PowerShell functions that will enable you to export the search customisations from the UI and the import them to the destination site using 1 line of PowerShell.

Open powershell and paste the following lines of code:

[reflection.assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”) | Out-Null
[reflection.assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.search”) | Out-Null

function Export-SPEnterpriseSearchSettings($siteUrl, $settingsFile=”SearchSchema.xml”, $exportLevel=[Microsoft.SharePoint.Client.Search.Administration.SearchObjectLevel]::SPSite){
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$searchConfigurationPortability = New-Object Microsoft.SharePoint.Client.Search.Portability.SearchConfigurationPortability($context)
$owner = New-Object Microsoft.SharePoint.Client.Search.Administration.SearchObjectowner($context,$exportLevel)
$value = $searchConfigurationPortability.ExportSearchConfiguration($owner)
$context.ExecuteQuery()
[xml]$schema = $value.Value
$schema.OuterXml | Out-File $settingsFile -Encoding UTF8
}

function Import-SPEnterpriseSearchSettings($siteUrl, $settingsFile=”SearchSchema.xml”, $exportLevel=[Microsoft.SharePoint.Client.Search.Administration.SearchObjectLevel]::SPSite){

$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$searchConfigurationPortability = New-Object Microsoft.SharePoint.Client.Search.Portability.searchconfigurationportability($context)
$owner = New-Object Microsoft.SharePoint.Client.Search.Administration.searchobjectowner($context,$exportLevel)
[xml]$schema = [xml](gc $settingsFile)
$searchConfigurationPortability.ImportSearchConfiguration($owner,$schema.OuterXml)
$context.ExecuteQuery()
}

Then type the following powershell command: Import-SPEnterpriseSearchSettings –siteUrl http://mysiteurl –settingsFile “C:\temp\SearchConfiguration.xml”

I have been asked about how to do this a few time’s so thought that this would be a good, quick post to write up.  Enjoy!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s