Karine Bosch’s Blog

On SharePoint

In search for my Search Service Application


Because on our default Search Service Application a lot of crawl rules are defined, we were not sure that our new SharePoint application would be crawled correctly during development. Therefore we decided to create a second Search Service Application to crawl our new site collection on our new Web Application. We also want to create our scopes during deployment.

We found a lot of posts that use the GetDefaultProxy method to get to the Search Service Application but in our case this piece of code always returned our first Search Service Application.

SPServiceContext serviceContext = SPServiceContext.GetContext(site);
SearchServiceApplicationProxy searchProxy = 
   serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy; 
if (searchProxy != null) 
{ 
   SearchServiceApplicationInfo searchServiceAppInfo = searchProxy.GetSearchServiceApplicationInfo(); 
   SearchServiceApplication searchServiceApp = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(
       searchServiceAppInfo.SearchServiceApplicationId); 
   Schema searchSchema = new Schema(searchServiceApp); 
}

My colleague Manoj Malireddy and I started looking into the Search topology and finally we came out with the following piece of code to get to the correct Search Service Application:

 SPServiceContext serviceContext = SPServiceContext.GetContext(site);
 SearchService searchService = new SearchService("OSearch14", SPFarm.Local);

 var srchApp = from SearchServiceApplication sapp in searchService.SearchApplications
               where sapp.Name == "Our other search service application"
               select sapp;

 SearchServiceApplication searchServiceApp = srchApp.First();
 searchSchema = new Schema(searchApplication);

This does not seemed to be the best way to get to a service application and after some time of research I found out that you can also write it like this, which seems a better way to me:

searchApplication = (SearchServiceApplication)SPFarm.Local.Services.
                    GetValue<SearchQueryAndSiteSettingsService>().
                    Applications.GetValue<SearchServiceApplication>("Our other search service application");

It took us several hours to find this out, but I hope that these snippets can save someone a few hours in the future.

July 17, 2013 - Posted by | Search

No comments yet.

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

%d bloggers like this: