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

上次修改时间: 2010年7月20日

适用范围: SharePoint Server 2010

备注

在此上下文中,客户端应用程序是指调用 Query Web 服务的应用程序。可以包括诸如 Microsoft ASP.NET Web 应用程序或 Windows 窗体应用程序这样的应用程序。

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

  • 设置客户端应用程序

  • 从客户端应用程序引用 Query Web 服务

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

  • 对客户端应用程序进行编码

  • 测试客户端应用程序

若要执行此演练,请确保:

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

  • 您具有对配置为使用 SharePoint Server 搜索的 SharePoint 网站的访问权。

设置客户端应用程序

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

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

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

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

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

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

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

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

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

  2. 将以下代码添加到 queryButton_Click 事件

    try
    {
    // Instantiate the Web service.
        QueryWebServiceProxy.QueryService queryService = new QueryWebServiceProxy.QueryService();
    // Use the credentials of the user running the client application. 
        queryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    //Execute the QueryEx method, returning the results to a DataSet
        System.Data.DataSet queryResults = queryService.QueryEx(GetXMLString());
    // Set the DataGridView data source to the first table in the DataSet oject, which contains the relevant results.
        resultsGrid.DataSource = queryResults.Tables[0];
    }
    catch (Exception ex)
    {
        resultsLabel.Text = ex.ToString();
    }
    
  3. 传递给 QueryEx 方法的字符串是在 GetXMLString 函数中构造的。将以下代码添加到 Form1 类可创建 GetXMLString 函数。

    private string GetXMLString()
    {
    /* 
       The proceeding six lines of code is actually one line of code.
       It is separated into four lines here for readability.
       You will need to remove the line breaks when you copy the code to your project.
    */
       StringBuilder xmlString = new StringBuilder("<QueryPacket xmlns='urn:Microsoft.Search.Query'>
    <Query><SupportedFormats><Format revision='1'> urn:Microsoft.Search.Response.Document:Document
    </Format></SupportedFormats><Context><QueryText language='en-US' type='STRING'>");
        xmlString.Append(queryTextBox.Text);
        xmlString.Append("</QueryText></Context></Query></QueryPacket>");
        return xmlString.ToString();
    }
    

测试客户端应用程序

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

  2. 在文本框中键入关键字查询。有关如何构造关键字查询的信息,请参阅关键字查询语法参考

  3. 单击"查询"向查询 Web Services 提交关键字查询。如果返回了结果,则将在 DataGridView 控件中显示这些结果。

请参阅

引用

QueryService

概念

使用查询 Web Services

生成搜索查询

其他资源

Microsoft.Search 架构引用