1

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!

  • If your CTX object point toward Site Collection? May be changing it to SubSite will give you desired result. – ThinkB4Code Jul 5 '17 at 14:50
  • Think about it as several document libraries existing within the same root site. so sub site has got nothing to do with it. – Jurgen Cuschieri Jul 5 '17 at 16:08
0

An effective way to go around this problem (rather than solve it), would be to use the "Path". The "Path" field is one of the 50 fields returned by the search, if Scenario 1 of the question is used. By specifying the library as part of the path, the results can be filtered after the search is executed rather than as part of the query.

var bypath = results.Value.FirstOrDefault().ResultRows.Where(c => c["Path"].ToString() == rootUrl + "/" + libraryName + "/" + keyword);

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.