SharePoint Search with Pagination


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
  1. /// <summary>
  2. /// search sharepoint using a predefined search query
  3. /// </summary>
  4. /// SQL like sharepoint search query
  5. /// SPSite to run against
  6. /// rowCount”>Optional: row limit
  7. /// <param name=”page”>Optional: page index</param>
  8. /// <returns></returns>
  9. public DataSet DefinedQuery(string query, SPSite site, int rowCount = 50, int page = 0)
  10. {
  11. DataSet dt = new DataSet();
  12. ServerContext srvContext = ServerContext.GetContext(site);
  13. int startRow = 0;
  14. if (page > 0)
  15. {
  16. startRow = page*rowCount;
  17. rowCount = rowCount * page;
  18. }
  19. //run query and pass values to allow for pagination
  20. using (FullTextSqlQuery runQry = new FullTextSqlQuery(srvContext)
  21. {
  22. ResultTypes = ResultType.RelevantResults,
  23. QueryText = query,
  24. RowLimit = rowCount,
  25. StartRow = startRow
  26. })
  27. {
  28. try
  29. {
  30. ResultTableCollection results = runQry.Execute();
  31. ResultTable resultTbl = results[ResultType.RelevantResults];
  32. //verify we have at least one result to work with
  33. if (resultTbl.RowCount >= 1)
  34. {
  35. //pull results in to our datatable and dataset
  36. DataTable tbl = dt.Tables.Add();
  37. tbl.Load(resultTbl);
  38. }
  39. resultTbl.Dispose();
  40. }
  41. catch (Exception ex)
  42. {
  43. string msg = ex.Message;
  44. }
  45. }
  46. return dt;
  47. }
  1. No comments yet.
(will not be published)


%d bloggers like this: