I want to replicate the search function but only return results which exist in a specific library. By using the below code I am managing to return the ListItem and load its folder and properties, but this is not working on a specific library but rather the whole the root site loaded in ClientContext ctx. The below code uses keyword which represents the name of the folder being searched for.
var keywordQuery =
new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ctx);
keywordQuery.QueryText = keyword;
keywordQuery.RowLimit = 500;
var searchExecutor =
new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ctx);
var results = searchExecutor.ExecuteQuery(keywordQuery);
ctx.ExecuteQuery();
var bytitle = results.Value.FirstOrDefault().ResultRows.Where(c => c["Title"].ToString() == keyword);
var li = list.GetItemByUniqueId(Guid.Parse(bytitle.First()["UniqueId"].ToString()));
ctx.Load(li); ctx.Load(li.Folder); ctx.Load(li.Folder.Files); ctx.Load(li.Folder.ListItemAllFields); ctx.ExecuteQuery();
I searched some sources online and I found that this QueryText:
keywordQuery.QueryText = "(ListID:{listguid} AND ContentClass:STS_ListItem ) " + keyword;
will filter on the library. However, the returned item in results is different than when the QueryText is simply set to the keyword. Essentially, the result returns the same item, but in the first scenario, the ResultRows are loaded with 50 fields which enable me to use the Title and UniqueId like below:
results.Value.FirstOrDefault().ResultRows.Where(c => c["Title"].ToString() == keyword);
whereas in the second scenario, the ResultRows are empty and further querying on specific fields can therefore not be done. These are the only properties that can be loaded and they are useless to me.
results.Value.FirstOrDefault().Properties
Any idea how to get the same ResultRows when filtering the search for a specific library? Thanks in advance!