演练:创建自定义企业级搜索 Web 部件

通过 Microsoft Office SharePoint Server 2007 企业级搜索,可以在浏览器中自定义搜索中心页和 Web 部件的外观和功能。但是,如果要做的自定义通过浏览器无法进行,则可以创建自定义 Web 部件,自定义 Web 部件使用查询对象模型对搜索组件执行查询。

在本演练中将创建自定义搜索 Web 部件,并将其添加到网站。本演练中介绍的 Web 部件只提供非常基础的搜索功能。

本演练涉及到以下任务:

  • 为 Web 部件创建项目

  • 编写 Web 部件的代码

  • 部署 Web 部件

先决条件

若要执行此演练,需要以下各项:

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

  • 在开发计算机上安装 Microsoft Office SharePoint Server 2007

  • 创建和修改搜索中心网站的权限

设置自定义搜索 Web 部件项目

为 Web 部件创建项目

  1. 在 Visual Studio 2005 的“文件”菜单上指向“新建”,然后单击“项目”。

  2. 在“项目类型”的“C#”下选择“Windows”。

  3. 在“模板”下选择“Web 控件库”。在“名称”字段中键入“CustomSearchWebPart”,然后单击“确定”。

接下来,必须向 Web 部件项目添加所需的引用。

备注

对于 Microsoft Office SharePoint Server 2007,所支持的开发环境配置是在安装了 Microsoft Office SharePoint Server 2007 的服务器上进行本地开发。

向 CustomSearchWebPart 项目添加引用

  1. 在“项目”菜单上单击“添加引用”。

  2. 在“.NET”选项卡上选择以下每个引用,并在选择每项后单击“确定”:

    • System.Data

    • System.XML

    • Microsoft.SharePoint

    • Microsoft.Office.Server

    • Microsoft.Office.Server.Search

为 Web 部件添加代码之前,将默认类文件替换为新的类文件。

为 Web 部件创建类文件

  1. 在“解决方案资源管理器”中右键单击 WebCustomControl1.cs,然后单击“删除”以删除随项目创建的默认类。

  2. 在“项目”菜单上单击“添加新项”。

  3. 在“添加新项”对话框中单击“Web 自定义控件”,键入“clsSearchQuery.cs”,然后单击“添加”。

编写自定义搜索 Web 部件代码

现在需要修改新的类文件中所包括的默认代码。

修改 clsSearchQuery 中的默认代码

  1. 在 clsSearchQuery.cs 中的代码顶部附近添加以下命名空间指令:

    using System.Drawing;
    using System.Data;
    using System.Xml;
    using System.Xml.Serialization;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.Office.Server;
    using Microsoft.Office.Server.Search.Query;
    
  2. 在以下代码行中将 WebControl 替换为 WebPart:

    public class clsSearchQuery : WebControl
    
  3. 在 clsSearchQuery 的类声明上方添加以下代码行:

    [XmlRoot(Namespace = "CustomSearchWebPart")]
    

现在已准备好,可以编写代码以查询搜索组件,然后呈现 Web 部件的内容。

添加并呈现 Web 部件的子控件

  1. 在类声明下方添加以下代码:

            Button cmdSearch;
            TextBox txtQueryText;
            Label lblQueryResult;
            DataGrid grdResults;
    
  2. 使用以下代码替代 CreateChildControls 方法:

    protected override void CreateChildControls()
            {
                Controls.Clear();
                txtQueryText = new TextBox();
                this.Controls.Add(txtQueryText);
                cmdSearch = new Button();
                cmdSearch.Text = "Start Search";
                cmdSearch.Click += new EventHandler(cmdSearch_Click);
                this.Controls.Add(cmdSearch);
                lblQueryResult = new Label();
                this.Controls.Add(lblQueryResult);
            }
    
  3. 使用以下代码为“cmdSearch” 添加单击事件:

            void cmdSearch_Click(object sender, EventArgs e)
            {
                if (txtQueryText.Text != string.Empty)
                {
                  keywordQueryExecute(txtQueryText.Text);
                }
                else
                {
                  lblQueryResult.Text = "You must enter a search word.";
                }
              }
    

现在已准备好,可以添加代码以访问查询对象模型。此代码示例使用 Microsoft.Office.Server.Search.Query.KeywordQuery 类执行搜索查询,同时为参数值传递 [Microsoft.Office.Server.ServerContext] 对象。此搜索查询是一个简单的关键字查询,搜索 Author 属性符合搜索词的结果。

执行关键字查询

  1. 将以下代码添加到 clsSearchQuery 类:

    private void keywordQueryExecute(string strQueryText)
            {
                KeywordQuery kRequest = new KeywordQuery(ServerContext.Current);
                string strQuery = "author:" + strQueryText;
                kRequest.QueryText = strQuery;
                //to return relevant results
                kRequest.ResultTypes |= ResultType.RelevantResults;
                ResultTableCollection resultTbls = kRequest.Execute();
                if ((int)ResultType.RelevantResults != 0)
                {
                    ResultTable tblResult = resultTbls [ResultType.RelevantResults];
                    if (tblResult.TotalRows == 0)
                    {
                      lblQueryResult.Text = "No Search Results Returned.";
                    }
                    else
                    {
                        ReadResultTable(tblResult);
                    }
    
                }
    }
    
  2. 为了读取搜索结果数据,keywordQueryExecute 方法调用 ReadResultTable 方法。为 ReadResultTable 添加以下代码:

    void ReadResultTable(ResultTable rt)
            {
                DataTable relResultsTbl = new DataTable();
                relResultsTbl.TableName = "Relevant Results";
                DataSet ds = new DataSet("resultsset");
                ds.Tables.Add(relResultsTbl);
                ds.Load(rt,LoadOption.OverwriteChanges,relResultsTbl);
                fillResultsGrid(ds);
            }
    
  3. ReadResultTable 方法调用 fillResultsGrid 方法以将搜索结果数据集绑定到 grdResults 数据网格。为 fillResultsGrid 添加以下代码:

    private void fillResultsGrid(DataSet grdDs)
            {
    //Instantiate the DataGrid, and set the DataSource
                grdResults = new DataGrid();
                grdResults.DataSource = grdDs;
    //Set the display properties for the DataGrid.
                grdResults.GridLines = GridLines.None;
                grdResults.CellPadding = 4;
                grdResults.Width = Unit.Percentage(100);
                grdResults.ItemStyle.ForeColor = Color.Black;
                grdResults.ItemStyle.BackColor = Color.AliceBlue;
                grdResults.ItemStyle.Font.Size = FontUnit.Smaller;
                grdResults.ItemStyle.Font.Name = "Tahoma";
                grdResults.HeaderStyle.BackColor = Color.Navy;
                grdResults.HeaderStyle.ForeColor = Color.White;
                grdResults.HeaderStyle.Font.Bold = true;
                grdResults.HeaderStyle.Font.Name = "Tahoma";
                grdResults.HeaderStyle.Font.Size = FontUnit.Medium;
    
    /*Turn off AutoGenerate for the columns, so that the DataGrid
    doesn't automatically bind to all of the columns
    in the search results set.
    Then create and configure only the columns you want to
    include in the DataGrid.
    */
                grdResults.AutoGenerateColumns = false;
                HyperLinkColumn colTitle = new HyperLinkColumn();
                colTitle.DataTextField = "Title";
                colTitle.HeaderText = "Title";
                colTitle.DataNavigateUrlField = "Path";
                grdResults.Columns.Add(colTitle);
                BoundColumn colAuthor = new BoundColumn();
                colAuthor.DataField = "Author";
                colAuthor.HeaderText = "Author";
                grdResults.Columns.Add(colAuthor);
    //Bind the data to the DataGrid
                grdResults.DataBind();
    //Add the DataGrid to the controls
                Controls.Add(grdResults);
            }
    

部署自定义搜索 Web 部件

由于在此示例中创建的自定义搜索 Web 部件不是强命名部件,因此不能将其部署到全局程序集缓存。必须将其部署到网站的 _app_bin 目录。

向网站部署自定义搜索 Web 部件

  1. 将 CustomSearchWebPart.dll 复制到网站的 _bin 目录。路径类似于以下内容:

    \Inetpub\wwwroot\wss\VirtualDirectories\Site\_app_bin
    

    备注

    如果不知道网站的应用程序路径,可以在 Internet 服务管理器中核实此路径。

  2. 打开 Internet 服务管理器控制台,展开“网站”节点,右键单击网站的应用程序,然后单击“属性”。

  3. 单击“主目录”选项卡。

    “本地路径”字段包含指向应用程序的路径。

注册自定义搜索 Web 部件作为 SafeControl

  1. 打开要向其添加自定义搜索 Web 部件的网站的 web.config 文件。可以在该网站的根文件夹中找到此文件。

  2. 将以下 <SafeControl/> 标记添加到 web.config 的 <SafeControls> </SafeControls> 部分:

    <SafeControl Assembly="CustomSearchWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="CustomSearchWebPart" TypeName="*" Safe="True" />
    
  3. 保存更改,然后关闭 web.config 文件。

创建自定义搜索 Web 部件定义文件

  1. 在文本编辑器(如记事本)中打开一个新文件,然后向该文件添加以下 XML 代码:

    <?xml version="1.0"?>
    <WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
       <Assembly>CustomSearchWebPart, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null</Assembly>
       <TypeName>CustomSearchWebPart.clsSearchQuery</TypeName>
       <Title>Custom Search Web Part</Title>
    </WebPart>
    
  2. 将文件命名为 CustomSearchWebPart.dwp,然后将其保存。

    备注

    如果不是在 Office SharePoint Server 2007 服务器上工作,则为完成此步骤,必须将文件复制到 Office SharePoint Server 2007 服务器。

注册自定义搜索 Web 部件作为 SafeControl

  1. 打开要向其添加自定义搜索 Web 部件的网站的 web.config 文件。

  2. 将以下 <SafeControl/> 标记添加到 web.config 的 <SafeControls> 部分:

    <SafeControl Assembly="CustomSearchWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="CustomSearchWebPart" TypeName="*" Safe="True" />
    

向网站部署自定义搜索 Web 部件

  • 将 CustomSearchWebPart.dll 复制到网站的 _bin 目录。路径类似于以下内容:

    \Inetpub\wwwroot\wss\VirtualDirectories\Site
    

Next Steps

示例:自定义企业级搜索 Web 部件代码 中可以找到 clsSearchQuery 类示例的完整代码。

部署 Web 部件后,即可对其进行测试。

测试自定义搜索 Web 部件

  1. 在浏览器中打开搜索中心网站,单击“网站操作”菜单,然后单击“创建页面”。

  2. 在“URL 名称”字段中键入“customsearchwebparttest”。

  3. 在“标题”字段中键入“自定义搜索 Web 部件的测试页”。

  4. 在“页面布局”列表中选择“(欢迎页面) 搜索页”。

  5. 若要创建页面,请单击“创建”。

  6. 在浏览器中打开 customsearchwebparttest.aspx 页,单击顶部区域的“添加 Web 部件”链接。

  7. 在“添加 Web 部件”对话框中单击“高级 Web 部件库和选项”链接。

  8. 单击“浏览”链接,然后单击“导入”。

  9. 单击“浏览”,导航到保存 CustomSearchWebPart.dwp 的位置,将其选中,然后单击“打开”。

    此时对话框中应该可以看到“自定义搜索 Web 部件”。可以将其拖到页面上使用。

See Also

任务

演练:创建 AdventureWorks 业务数据应用程序示例的 ASP.NET Web 部件

参考

企业级搜索关键字语法参考

Microsoft.Office.Server.Search.Query.KeywordQuery

概念

自定义企业级搜索 Web 部件入门

企业级搜索查询对象模型概述