private Guid FetchContentSourceID(SPSite site, string contentSourceName)
{
var serviceContext = SPServiceContext.GetContext(site);
var proxy = (SearchServiceApplicationProxy)serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy));
var federationManager = new FederationManager(proxy);
var searchObjectOwner = new SearchObjectOwner(SearchObjectLevel.SPWeb, site.RootWeb);
var searchObjectFilter = new SearchObjectFilter(searchObjectOwner) { IncludeHigherLevel = true };
var contentSources = federationManager.ListSources(searchObjectFilter , false);
var contentSource = (from source in contentSources
where source.Name == contentSourceName
select source).FirstOrDefault();
if (contentSource != null)
{
return contentSource .Id;
}
else
{
return Guid.Empty;
}
}Content source ID is required when we use KeywordQuery class from Server Side Object Model. We need to set the SourceId property of KeywordQuery class.
