Monday, September 1, 2014

Importing Search Configurations on the SSA

imageYou might have noticed the settings for site collections have options to export and import search configuration files, which is very nice indeed. The Search Service Application however, does not have these options. What is available is the use of CSOM to export the SSA configuration via the SearchConfigurationPortability class. If you try to import a configuration file to the SSA you will simply get a nice error, explained at the documentation page for SearchConfigurationPortability with this interesting quote: ….however, you cannot import customized search configuration settings to a Search service application (SSA).

 Note: This does not apply to SharePoint Online where you can import/export search configuration from the UI for your tenant.

So, you have export, but no import!


But I need import in my current project as doing PowerShell/SSOM for each rule manually is just not feasible. Too many moving parts, and parsing some new configuration format to cover all options is way more work than I’m willing to invest.

We already have scripts in my project to do managed property to crawled property mappings, which is good as this post won’t solve that issue (no way to override import of master schema – I did try).

However what I will show is how you can import:

  • Query Rules
  • Result Sources
  • Result Types

With some help from my trusted old friend Reflector I mixed up the following piece of code (using SSOM) to import a search configuration file to the Search Service Application. Enjoy!

The procedure I usually follow is to create a blank site collection, configure my result types, result sources and query rules, then export the config to an xml file which I can import somewhere else.

Assembly asm = Assembly.Load("Microsoft.Office.Server.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
Type searchConfigurationSettingsType = asm.GetType("Microsoft.Office.Server.Search.Portability.SearchConfigurationSettings");
PropertyInfo p1 = searchConfigurationSettingsType.GetProperty("SearchQueryConfigurationSettings");

DataContractSerializer serializer = new DataContractSerializer(searchConfigurationSettingsType);
// _config contains the XML from an exported search configuration file
XmlReader reader = XmlReader.Create(new StringReader(_config));

object readObject = serializer.ReadObject(reader, true);
List<SearchQueryConfigurationSettings> queryConfigurationSettings = p1.GetValue(readObject) as List<SearchQueryConfigurationSettings>;

// Get SP context
SPServiceContext serviceContext = SPServiceContext.GetContext(new SPSite("http://central.admin"));
ISearchServiceApplication proxy = SearchServiceApplicationProxy.GetProxy(serviceContext);

ExportImportManager manager = new ExportImportManager((SearchServiceApplicationProxy) proxy);
SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.Ssa);

if (queryConfigurationSettings != null) {
foreach (var queryConfigurationSetting in queryConfigurationSettings)
// Import the configuration
manager.ImportQueryConfiguration(owner, queryConfigurationSetting, null);
// Use the line below to remove the configuration
//manager.DeleteQueryConfiguration(owner, queryConfigurationSetting);