Sunday, August 2, 2015

Importing managed properties and search settings to your SharePoint Online tenant is dead easy!

The crux to get this working is to set the client context to your sharepoint admin site and use the SPSiteSubscription level for your import.

string adminUrl = "https://delve-admin.sharepoint.com";
using (var context = new ClientContext(adminUrl))
{
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
    SearchConfigurationPortability searchConfig = new SearchConfigurationPortability(context);
    SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPSiteSubscription);
    string xml = System.IO.File.ReadAllText("SearchConfiguration.xml");
    searchConfig.ImportSearchConfiguration(owner, xml);
    context.ExecuteQuery();
}

If you are using OfficePnP then you get away with the following snippet

string adminUrl = "https://delve-admin.sharepoint.com";
using (var context = new ClientContext(adminUrl))
{
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
    context.ImportSearchSettings("SearchConfiguration.xml", SearchObjectLevel.SPSiteSubscription);
}