SharePoint Blog

How to Programmatically find Search Service Application in SharePoint 2013

Leave a comment

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(
Schema searchSchema = new Schema(searchServiceApp);

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.
Applications.GetValue(“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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s