Looking to retrieve the SearchServiceApplication object for the current site? It’s pretty straight forward, as the following demonstrates:
Start by adding the following namespace to your class
using Microsoft.Office.Server.Search.Administration; |
The Microsoft.Office.Server assembly can be found in the ISAPI folder of the SharePoint hive.
To get the instance of the SearchServiceApplication for the current site we first need to get the SPServiceContext for the current site. Next, using this context object, we retrieve the proxy class for the SearchServiceApplication, which is called SearchServiceApplicationProxy.
Using the proxy class the next step is to retrieve the application identifier for the search service application service, which we will then use to retrieve the SearchServiceApplication instance for the site.
After this we will have the current site’s SearchServiceApplication class instance object.
// Get the service context for the current site SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site); // Get the SearchApplicationProxy object from service context object SearchServiceApplicationProxy searchApplicationProxy = serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy; // Get the identifier for the search service application from the proxy class retrieved above. Guid applicationId = searchApplicationProxy.GetSearchServiceApplicationInfo().SearchServiceApplicationId; // Lastly, retreieve the SearchServiceApplication object for the current site. SearchServiceApplication searchApplication = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(applicationId); |
Thanks a bunch! Saved me a ton of time!