|
private static ResultTable GetSearchResults(int startIndex,ClientContext clientContext) |
|
{ |
|
ClientResult<ResultTableCollection> results = null; |
|
try |
|
{ |
|
KeywordQuery keywordQuery = new KeywordQuery(clientContext); |
|
|
|
SearchExecutor searchExecutor = new SearchExecutor(clientContext); |
|
|
|
keywordQuery.StartRow = startIndex;//gets or sets the first row of information from the search results |
|
|
|
keywordQuery.QueryText = "queryText here"; |
|
|
|
keywordQuery.RowLimit = 500; |
|
|
|
keywordQuery.RowsPerPage = 500; |
|
|
|
keywordQuery.TrimDuplicates = false; |
|
|
|
keywordQuery.Timeout = 10000; //10 minutes |
|
|
|
// execute the query and load the results into a collection |
|
results = searchExecutor.ExecuteQuery(keywordQuery); |
|
|
|
clientContext.ExecuteQuery(); |
|
|
|
//clientContext.ExecuteQueryWithIncrementalRetry(5, 30000); //5 retries, with a base delay of 30 secs. |
|
|
|
return results.Value.FirstOrDefault(v => v.TableType.Equals(KnownTableTypes.RelevantResults)); |
|
} |
|
catch (Exception) |
|
{ |
|
throw; |
|
} |
|
} |
|
|
|
static void Main(string[] args) |
|
{ |
|
using (ClientContext clientContext = new ClientContext("https://sitecollectionurl")) |
|
{ |
|
#region Build Data Dable |
|
DataTable resultDataTable = new DataTable(); |
|
|
|
DataColumn titleCol = new DataColumn("Title"); |
|
DataColumn pathCol = new DataColumn("Path"); |
|
|
|
resultDataTable.Columns.Add(titleCol); |
|
resultDataTable.Columns.Add(pathCol); |
|
|
|
#endregion |
|
|
|
int currentRowIndex = 0; |
|
|
|
var resultTable = GetSearchResults(0, clientContext); |
|
|
|
if (resultTable != null && resultTable.TotalRowsIncludingDuplicates > 0) |
|
{ |
|
while (resultTable.TotalRowsIncludingDuplicates > resultDataTable.Rows.Count) |
|
{ |
|
foreach (var resultRow in resultTable.ResultRows) |
|
{ |
|
DataRow row = resultDataTable.NewRow(); |
|
row["Title"] = resultRow["Title"]; |
|
row["Path"] = resultRow["Path"]; |
|
resultDataTable.Rows.Add(row); |
|
} |
|
|
|
//Update the current row index |
|
currentRowIndex = resultDataTable.Rows.Count; |
|
|
|
resultTable = null; |
|
|
|
resultTable = GetSearchResults(currentRowIndex, clientContext); |
|
|
|
if (resultTable != null && resultTable.TotalRowsIncludingDuplicates > 0) |
|
{ |
|
if (resultTable.RowCount <= 0) |
|
break; |
|
} |
|
else |
|
break; |
|
} |
|
} |
|
|
|
string totalResults = resultDataTable.Rows.Count; |
|
} |
|
|
|
} |