Get ResultTable from ResultTableCollection in SharePoint 2010
In order to get a ResultTable
object from a ResultTableCollection
(typically obtained from a search query execution using the KeywordQuery
class) you should use the property Item
from the class ResultTableCollection
documented here.
Through the access operator [ ]
you must specify RelevantResults
as ResultType
:
ResultTable restab = results[ResultType.RelevantResults];
Into the ResultTable
object you will get a table filled with search results.
Below a code snippet for counting the total number of results for a given query text:
public int SearchResultCount(string queryText)
{
int searchResultCount = 0;
using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url))
{
KeywordQuery query = new KeywordQuery(site);
query.ResultsProvider = SearchProvider.Default;
query.ResultTypes = ResultType.RelevantResults;
query.KeywordInclusion = KeywordInclusion.AllKeywords;
query.QueryText = queryText;
ResultTableCollection results = query.Execute();
if (results.Count > 0)
{
ResultTable restab = results[ResultType.RelevantResults];
searchResultCount = restab.TotalRows;
}
} // using site
return searchResultCount;
}
References
- http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/08/12/how-to-use-the-sharepoint-2010-enterprise-search-keywordquery-class.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.office.server.search.query.resulttablecollection_members%28v=office.14%29.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.search.query.keywordquery_members%28v=office.15%29.aspx