1

I have a requirement where I'm using a search api in my C# code and am fetching the data. Now I'm stuck in iterating the data. Can someone help me on this or is there some way through which we can achieve this functionality to get the items using search api in C#

Below is the code that I'm using,This is returning me the total item count, but is not returning the items in the result. Total item Count that I'm getting is

"TotalRows": 2802, "TotalRowsIncludingDuplicates": 2802

But items that I'm getting is only 40, Can someone please help on this

  KeywordQuery keywordQuery = new KeywordQuery(clientContext);  
                keywordQuery.SourceId = new Guid("78b793ce-7956-4669-aa3b-451fc5defebf");
                keywordQuery.QueryText = "videos";
                keywordQuery.StartRow = 0;
                keywordQuery.RowLimit = 5000;
                keywordQuery.EnableStemming = true;
                keywordQuery.TrimDuplicates = false;
                SearchExecutor searchExecutor = new SearchExecutor(clientContext);  
                ClientResult < ResultTableCollection > results = searchExecutor.ExecuteQuery(keywordQuery);  
                clientContext.ExecuteQuery(); 
2

Here is the working code that I have already used recently:

ResultTableCollection searchResults = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = searchResults.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
DataTable resultDataTable = resultTable.Table;
int currentRowIndex = 0;
while (resultTable.TotalRowsIncludingDuplicates > resultDataTable.Rows.Count)
{
    currentRowIndex = resultDataTable.Rows.Count;
    searchResults = GetSearchResults(keywordQuery, currentRowIndex, searchExecutor);
    var searchResults1 = searchResults.FirstOrDefault();
    if (searchResults1.RowCount <= 0 || resultDataTable.Rows.Count >=rowLimit)
        break;
    else
        resultDataTable.Merge(searchResults1.Table);
    }
}

It will query and get all items from SharePoint search Results

1

hi you need to iterate through < ResultTableCollection > results to get the data.

KeywordQuery keywordQuery = new KeywordQuery(clientContext);  
keywordQuery.SourceId = new Guid("78b793ce-7956-4669-aa3b-451fc5defebf");
keywordQuery.QueryText = "videos";
keywordQuery.StartRow = 0;
keywordQuery.RowLimit = 5000;
keywordQuery.EnableStemming = true;
keywordQuery.TrimDuplicates = false;
SearchExecutor searchExecutor = new SearchExecutor(clientContext);  
ClientResult < ResultTableCollection > results = 
searchExecutor.ExecuteQuery(keywordQuery);  
clientContext.ExecuteQuery(); 
foreach(ResultTable sr in results.Value)
{
foreach(var row in sr.ResultRows)
 {
    //your code to have data from row
    Console.WriteLine("{0}: {1} ({2})", row["Title"], row["Path"], row["Write"]);
 }
}

For reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.