0

Below is my code to search for a keyword

using ( ClientContext clientContext = new ClientContext ( new Uri ( "https://company.sharepoint.com/sites/records/" ) ) )
{
    clientContext.Credentials = Credentials;
    KeywordQuery keywordQuery = new KeywordQuery ( clientContext );
    keywordQuery.QueryText = "SharePoint";
    keywordQuery.TrimDuplicates = false;

    // additional properties for keywordQuery which limit the results to particulat document library

    SearchExecutor searchExecutor = new SearchExecutor ( clientContext );
    ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery ( keywordQuery );
    clientContext.ExecuteQuery ( );

    ResultTable resultTable = results.Value.FirstOrDefault ( );

}

I could barely find any working examples over the internet which restricts the results to only one particular document library.

3

I can think if two easy ways to do it.

  1. Create a result source: Configure the below query to result source

{searchTerms} (ListID:{library guid here} AND ContentClass:STS_ListItem )

Now set the KeyWordQuery to use the result source.

keywordQuery.SourceId="id of result source";

2. Just add the result source query directly into the query text.

keywordQuery.QueryText="(ListID:{library guid here} AND ContentClass:STS_ListItem ) SharePoint";

1
  • I quickly tried the second way and it works. I will try to test the first way when I have some time. Meanwhile I am marking this as the answer. Thanks. Sep 8 '16 at 22:08

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.