示例:自定义企业级搜索 Web 部件代码

以下是 clsSearchQuery.cs 示例完整的示例代码,如演练:创建自定义企业级搜索 Web 部件中所述。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;


namespace CustomSearchWebPart
{
    
    [ToolboxData("<{0}:clsSearchQuery runat=server></{0}:clsSearchQuery>")]
    [XmlRoot(Namespace = "CustomSearchWebPart")]
    public class clsSearchQuery : WebPart
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        Button cmdSearch;
        TextBox txtQueryText;
        Label lblQueryResult;
        DataGrid grdResults;

        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);
 
        }

        void cmdSearch_Click(object sender, EventArgs e)
        {
            if (txtQueryText.Text != string.Empty)
            {
                keywordQueryExecute(txtQueryText.Text);

            }
            else
            {
                lblQueryResult.Text = "You must enter a search word.";
            }
            
        }


        //Execute Keyword Query
     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);
                }

            }
  }

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);
        }

        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 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);
        }
    }
}

See Also

概念

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