Exporting and importing search configuration settings in SharePoint

Get code examples that show you how to export and import customized search configuration settings. These settings include all customized query rules, result sources, result types, ranking models, and site search settings. SharePoint exposes this functionality through the Microsoft.Office.Server.Search.Portability namespace.You can also export customized search configuration settings from a Search service application (SSA) and import the settings to site collections and sites.

Note

You can't import customized search configuration settings to an SSA, or export the default search configuration settings.

Export search configuration settings

The following code shows how to use SearchConfigurationPortability to export your site's search configuration settings. The code uses an example site http://yoursite/sites/publishing1, which you'd replace with your own site. fileName refers to the file where the search configuration settings are stored; owner specifies the SPWeb level at which the search configuration settings are obtained.


private static void Export(string fileName)
{
    SPSite site = new SPSite("http://yoursite/sites/publishing1");
    SearchConfigurationPortability conf = new SearchConfigurationPortability(site);
    SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPWeb, site.OpenWeb());
    var buff = conf.ExportSearchConfiguration(owner);
    File.WriteAllText(fileName, buff);
    site.Close();
}

Import search configuration settings

The following code shows how to import search configuration settings from a file by using SearchConfigurationPortability and replace the existing search settings on a specified site, http://yoursite/sites/publishing1. fileName refers to the file where the search configuration settings are stored; owner specifies the SPWeb level at which the search configuration settings are obtained.


private static void Import(string fileName)
{
    SPSite site = new SPSite("http://yoursite/sites/publishing1");
    SearchConfigurationPortability conf = new SearchConfigurationPortability(site);
    SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPWeb, site.OpenWeb());
    conf.ImportSearchConfiguration(owner, File.ReadAllText(fileName));
    site.Close();
}

See also