演练:从客户端应用程序查询 FAST Search Server

查询 Web Services 向客户端应用程序公开 Microsoft SharePoint Server 2010 中的 SharePoint 企业级搜索功能。

上次修改时间: 2011年9月1日

备注

本文内容
先决条件
步骤 1:安装和配置基于窗体的客户端应用程序
步骤 2:为客户端应用程序编写代码
步骤 3:测试客户端应用程序
在此上下文中,客户端应用程序是指调用查询 Web Services 的应用程序。可以包括诸如 Microsoft ASP.NET Web 应用程序或 Windows 窗体应用程序这样的应用程序。

查询 Web Services 的 QueryEx Web 方法向搜索服务发送查询并返回 DataSet 对象中的结果。以下演练介绍如何使用查询 Web Services,以通过使用 QueryEx Web 方法将 FAST Search Server 2010 for SharePoint 结果返回到基于 Windows 的客户端应用程序,并包含以下任务:

本演练是对演练:从客户端应用程序查询 SharePoint 搜索的扩展。Visual Studio 2010 项目会添加第二个"DataGridView"控件,而且代码示例使用 FAST 查询语言 (FQL) 查询语法。

适用范围: SharePoint Server 2010

先决条件

若要完成本演练,请确保:

  • Microsoft Visual Studio 2010 安装在开发计算机上

  • 您有权访问配置为使用 FAST Search Server 2010 for SharePoint 的 SharePoint Server 2010 网站

步骤 1:安装和配置基于窗体的客户端应用程序

在 Visual Studio 2010 中安装基于窗体的客户端应用程序,其中包括以下元素:

  • 用于查询输入的文本框。

  • 查询按钮。

  • 用于显示查询结果表的"DataGridView"控件。

  • 用于显示其他查询结果表的"DataGridView"控件。该控件不在本文的代码示例中使用,但在诸如查询优化 (FAST Search Server 2010 for SharePoint)这样的其他文章中使用。

安装 Visual Studio 项目

  1. 在 Visual Studio 2010 中的"文件"菜单上,指向"新建",然后单击"项目"。

  2. 在"已安装的模板"下,展开"Visual C#",然后单击"Windows"。

  3. 选择"Windows 窗体应用程序"。在"名称"字段中键入 QueryExClientSample,然后单击"确定"。

添加对查询 Web Services 的 Web 引用

  1. 在"解决方案资源管理器"中,右键单击项目名称,再单击"添加服务引用"。

  2. 在"添加服务引用"对话框中,单击"高级"。

  3. 在"服务引用设置"对话框中,单击"添加 Web 引用"。

  4. 在"添加 Web 引用"对话框的"URL"文本字段中,键入以下地址:http://SERVER/_vti_bin/search.asmx。将 SERVER 替换为 SharePoint 网站的 URL,然后单击"转到"。

  5. 当找到该 Web 服务时,"添加 Web 引用"对话框的主窗口中会显示 QueryService Web 服务的页面。在"Web 引用名"字段中键入 QueryWebServiceProxy,然后单击"添加引用"。

修改客户端应用程序的默认窗体

  1. 在"解决方案资源管理器"中,双击该窗体(如果使用的是默认窗体,则为"Form1")。

  2. 添加 Query 按钮:

    在"工具箱"中,展开"公共控件",单击"Button",然后将该控件拖动到窗体中。在"属性"中,将"(Name)"更改为 queryButton,然后在"Text"属性中键入 Query。

  3. 添加查询输入框:

    在"工具箱"中,单击"TextBox",然后将该控件拖动到窗体中。在"属性"中,将"(Name)"更改为 queryTextBox,然后将"Multiline"设置为"True"。

  4. 添加用于显示查询结果表的"DataGridView"控件:

    在"工具箱"中,展开"Data",单击"DataGridView",然后将该控件拖动到窗体中。在"属性"中,将"(Name)"更改为 resultsGrid。

  5. 添加第二个用于显示其他查询结果表的"DataGridView"控件:

    在"工具箱"中,展开"Data",单击"DataGridView",然后将该控件拖动到窗体中。在"属性"中,将"(Name)"更改为 secondGrid。

  6. 添加错误消息"Label"控件:

    在"工具箱"中,单击"Label",然后将该控件拖动到窗体中。在"属性"中,将"(Name)"更改为 resultsLabel,然后删除"Text"属性的内容。

步骤 2:为客户端应用程序编写代码

该代码实现与您已配置的"queryButton"关联的查询处理程序。该代码执行以下任务:

  • 设置查询 Web Services 连接

  • 创建查询 XML

  • 在 FQL STRING 运算符中打包用户输入的查询

  • 使用 QueryEx 查询 Web Services 方法运行查询

  • 在第一个"DataGridView"控件中显示查询结果

  • 在"MessageBox"中输出查询结果的扩展属性

为客户端应用程序编写代码

  1. 双击"Query"按钮为 Click 事件添加事件处理程序。代码编辑器将打开,并且光标位于 queryButton_Click 事件处理程序内。

  2. 添加以下命名空间引用。

    using System.Collections;
    
  3. 将以下代码添加到 queryButton_Click 事件。

    try
    {
       // Instantiate the Query Web service.
       QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
       // Use the credentials of the user running the client application: 
       queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
       // Run the QueryEx method, returning the results to a DataSet:
       System.Data.DataSet queryResults = queryService.QueryEx(GetXMLString());
       // Set the first DataGridView data source to the RelevantResults table in the DataSet object:
       resultsGrid.DataSource = queryResults.Tables["RelevantResults"];
    
       // Print extended properties for the result set in a message box.
       StringBuilder props = new StringBuilder("Extended properties on result object:\n");
       foreach (DictionaryEntry de in queryResults.ExtendedProperties)
       {
          props.AppendFormat("{0}: {1}\n", de.Key, de.Value);
       }
       props.Append("\nExtended properties on RelevantResults:\n"); 
       foreach (DictionaryEntry de in queryResults.Tables["relevantresults"].ExtendedProperties)
       {
          props.AppendFormat("{0}: {1}\n", de.Key, de.Value);
       }
       MessageBox.Show(props.ToString());
     }
    catch (Exception ex)
    {
       resultsLabel.Text = ex.ToString();
    }
    
  4. 在 GetXMLString 函数中构造传递给 QueryEx 方法的查询 XML 字符串。

    将以下代码添加到 Form1 类可创建 GetXMLString 函数。

    // Build the query XML string.
    private string GetXMLString()
    {
       // queryXML1 is the part of the XML before the query string.
       string queryXML1 = @"<QueryPacket xmlns='urn:Microsoft.Search.Query'>
       <Query>
          <SupportedFormats>
             <Format revision='1'> urn:Microsoft.Search.Response.Document:Document</Format>
          </SupportedFormats>
          <Context>
             <QueryText language='en' type='FQL'>";
    
       // queryXML2 is the part of the XML after the query string.
       string queryXML2 = @"
             </QueryText>
          </Context>
          <ResultProvider>FASTSearch</ResultProvider>
          <Range>
             <Count>10</Count>
          </Range>
       </Query>
    </QueryPacket>";
    
    // Build the Query XML string.
       StringBuilder xmlString = new StringBuilder(queryXML1); 
       xmlString.Append(GetFQLString());
       xmlString.Append(queryXML2);
       return xmlString.ToString();
    }
    
    // Build the FQL query string.
    // string("<user-typed query>", mode="and")
    private string GetFQLString()
    {
       StringBuilder fqlString = new StringBuilder("string(\"");
       fqlString.Append(queryTextBox.Text);
       fqlString.Append("\", mode=\"and\")");
       return fqlString.ToString();
    }
    

步骤 3:测试客户端应用程序

通过在查询框中键入一个或多个查询词来测试客户端应用程序。查询表示您所键入的词语的 AND。

测试客户端应用程序

  1. 按 F5 生成并运行客户端应用程序。

  2. 在文本框中键入一个或多个词语。

  3. 单击"查询"向查询 Web Services 提交查询。如果返回了结果,则将在第一个"DataGridView"控件中显示这些结果。扩展属性将在"MessageBox"弹出窗口中显示。

请参阅

引用

QueryService

概念

使用查询 Web Services

演练:从客户端应用程序查询 SharePoint 搜索

FAST 查询语言 (FQL) 语法参考

生成搜索查询

其他资源

Microsoft.Search 架构引用