This is an example class for showing how to search SharePoint with paging support in the API. This function accepts a string containing the standard SharePoint SQL style search query:
ie: SELECT TOP 50 AccountName, Size, Rank, Path, Title, Description, Write FROM portal..scope() WHERE “scope” = ‘People’ AND CONTAINS (“LastName”,'”Doe”‘) ORDER BY “AccountName” DESC
To page you can use the optional int rowCount and page overloads. Whenever page is specified it judges the start row as rowCount*page. So page 10 begins at row 500 if rowCount is 50.
Paged Search Example
-
/// <summary>
-
/// search sharepoint using a predefined search query
-
/// </summary>
-
/// SQL like sharepoint search query
-
/// SPSite to run against
-
/// rowCount”>Optional: row limit
-
/// <param name=”page”>Optional: page index</param>
-
/// <returns></returns>
-
public DataSet DefinedQuery(string query, SPSite site, int rowCount = 50, int page = 0)
-
{
-
ServerContext srvContext = ServerContext.GetContext(site);
-
int startRow = 0;
-
if (page > 0)
-
{
-
startRow = page*rowCount;
-
rowCount = rowCount * page;
-
}
-
//run query and pass values to allow for pagination
-
{
-
ResultTypes = ResultType.RelevantResults,
-
QueryText = query,
-
RowLimit = rowCount,
-
StartRow = startRow
-
})
-
{
-
try
-
{
-
ResultTableCollection results = runQry.Execute();
-
ResultTable resultTbl = results[ResultType.RelevantResults];
-
//verify we have at least one result to work with
-
if (resultTbl.RowCount >= 1)
-
{
-
//pull results in to our datatable and dataset
-
DataTable tbl = dt.Tables.Add();
-
tbl.Load(resultTbl);
-
}
-
resultTbl.Dispose();
-
}
-
catch (Exception ex)
-
{
-
string msg = ex.Message;
-
}
-
}
-
return dt;
-
}