How To Migrate SharePoint Search Properties From One Tenant To Another Using CSOM

This article explains how to move a SharePoint Search managed property from a tenant to another using CSOM.

SharePoint is a web portal which acts as a collaborative platform to manage, search, and share contents. SharePoint search is an excellent and highly powerful search. Search schemas can be configured and managed in SharePoint to make it more efficient based on our needs. We can create search properties through this search schema and map it to a list column or an existing user profile property. Those  contents will be crawled and will be available for the user search. SharePoint on-premise has the option to crawl the properties and contents manually or automatically. But in SharePoint online, it has to be performed automatically. So, we need to wait for an hour or two to make  content available for search, after adding it. In case we need to perform a search crawl manually, we can always contact Office 365 support team.

Create a Search Property:

First, we need to know how to create a search property before migrating it.
  • We should log into the SharePoint Admin Center.

  • Click Search from the left menu.

    SharePoint

  • Select “Manage Search Schema”

    SharePoint

  • Select “New Managed Property”

    SharePoint
  • Then give the Property Name, Description, Type of the property and select the Searchable, Retrievable checkbox.

  • In the mapping to crawled property section, select “Add a Mapping” to choose the property or column which should be included inside the search. Then, select Ok to create it.

Migrating Search Crawl Property:

After creating the property, create a windows application and add “Microsoft.SharePointOnline.CSOM” Nuget package to the solution. We have to perform Export and Import operation to perform the migration. For exporting the search managed property use the below code snippet.

  1. private static string exportSearchSettings(ClientContext clientContext) {  
  2.     SearchConfigurationPortability searchConfiguration = null;  
  3.     SearchObjectOwner searchObjectOwner = null;  
  4.     ClientResult < string > configResults = null;  
  5.     string stringresult = string.Empty;  
  6.     try {  
  7.         searchConfiguration = new SearchConfigurationPortability(clientContext);  
  8.         searchObjectOwner = new SearchObjectOwner(clientContext, SearchObjectLevel.SPSiteSubscription);  
  9.         configResults = searchConfiguration.ExportSearchConfiguration(searchObjectOwner);  
  10.         clientContext.ExecuteQuery();  
  11.         if (configResults.Value != null) stringresult = configResults.Value;  
  12.     } catch (Exception) {  
  13.         throw;  
  14.     }  
  15.     return stringresult;  
  16. }  
From the above code, we will be getting an xml string which can be imported to another site. After the above code execution, make sure that you’re storing the content somewhere to reuse it. I am saving the document as a file inside the application as “searchConfiguration.xml”

After storing it, use the below code to migrate the configurations to a new tenant.

  1. private static void importSearchSettings(ClientContext clientContext) {  
  2.     SearchConfigurationPortability searchConfiguration = null;  
  3.     SearchObjectOwner searchObjectOwner = null;  
  4.     string location = string.Empty, directory = string.Empty;  
  5.     try {  
  6.         location = Assembly.GetExecutingAssembly().Location;  
  7.         directory = Path.GetDirectoryName(location);  
  8.         searchConfigurationString = System.IO.File.ReadAllText(directory + "/searchConfiguration.xml");  
  9.         searchConfiguration = new SearchConfigurationPortability(clientContext);  
  10.         searchObjectOwner = new SearchObjectOwner(clientContext, SearchObjectLevel.SPSiteSubscription);  
  11.         searchConfiguration.ImportSearchConfiguration(searchObjectOwner, searchConfigurationString);  
  12.         clientContext.ExecuteQuery();  
  13.     } catch (Exception) {  
  14.         throw;  
  15.     }  
  16. }  

I hope you learned how to migrate search properties from one tenant to another. Feel free to fill my comment box with your thoughts and doubts on this article.

Happy SharePointing!!!