[C#.NET][Sharepoint 2013] 如何使用 CSOM KeywordQuery 進行全文檢索

[C#.NET][Sharepoint 2013] 如何使用 CSOM KeywordQuery 進行全文檢索

準備工作

有關Crawl 請參考

http://technet.microsoft.com/en-us/library/jj219814%28v=office.15%29.aspx

  • 加入 Microsoft.SharePoint.Client.Search.dll

目前我的開發環境有兩個版本

      1. C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI
      2. C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI

以下元件參考要跟 Microsoft.SharePoint.Client.Search.dll 版本一樣

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

 

Enable Crawl

@CA

image

image

image

image

 

 

 

 

 

Enable continuous crawls

@CA

image

image

 

進行搜尋,如下圖:

image

 

列出結果,如下圖:

image

 

搜尋關鍵字程式碼如下:

  1. 搜尋定義 KeywordQuery
  2. 執行搜尋SearchExecutor.ExecuteQuery

 

 

 


{
    using (ClientContext client = new ClientContext("http://sps2013/CsomApi"))
    {
        client.Credentials = new NetworkCredential(YourAccount, YourPassWord);
        var query = new KeywordQuery(client);
        query.QueryText = "demo";

        var search = new SearchExecutor(client);
        var result = search.ExecuteQuery(query);

        client.ExecuteQuery();

        foreach (var item in result.Value[0].ResultRows)
        {
            Console.WriteLine("title:{0} \t path:{1}", item["Title"], item["Path"]);
        }
        Console.Read();
    }
}

執行結果如下圖,這跟在 Sharepoint 所執行的結果一樣

image

 


本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/05/145394.aspx

 

參考文章:

http://blogs.technet.com/b/tothesharepoint/archive/2013/10/31/how-to-create-a-search-center-site-collection-and-enable-crawling-of-your-content-in-sharepoint-2013.aspx

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo