Condividi tramite


Code Sample: Custom Federated Search Web Part Code

Applies to: SharePoint Server 2010

The following is the complete sample code for the Custom Federated Search Web Part, described in Walkthrough: Creating a Basic Search Web Part Using the Federation Object Model.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;
using System.Xml;

namespace CustomFederatedSearch.Custom_Federated_Search
{
      [ToolboxItemAttribute(false)]
     public class Custom_Federated_Search : WebPart
     {
          Button queryButton;
          TextBox queryTextBox;
          Label resultsLabel;
          DataGrid resultsGrid;

          protected override void CreateChildControls()
          {
               Controls.Clear();
               queryTextBox = new TextBox();
               this.Controls.Add(queryTextBox);
               queryButton = new Button();
               queryButton.Text = "Start Search";
               queryButton.Click += new EventHandler(queryButton_Click);
               this.Controls.Add(queryButton);
               resultsLabel = new Label();
               this.Controls.Add(resultsLabel);
          }

          void queryButton_Click(object sender, EventArgs e)
          {
               if (queryTextBox.Text != string.Empty)
               {
                    ExecuteFederatedQuery(queryTextBox.Text);
               }
               else
               {
                    resultsLabel.Text = "You must enter a search word.";
               }
          }

          void ExecuteFederatedQuery(string queryText)
          {
               long lastupdate;
               bool crawl;

               SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
(SPServiceContext.GetContext(SPContext.Current.Site));
               LocationConfiguration[] locationConfigs;
               LocationConfiguration locationConfig = null;
               locationConfigs = proxy.GetLocationConfigurations(out lastupdate, out crawl);
               Location location;
               LocationList locationList = new LocationList();
               QueryManager queryManager = new QueryManager();
               foreach (LocationConfiguration locConfig in locationConfigs)
               {
                    if (locConfig.InternalName.Equals("LocalSearchIndex"))
                    {
                         locationConfig = locConfig;
                         break;
                    }
               }
               location = new Location(locationConfig.InternalName, proxy);
               location.UserQuery = queryText;
               locationList.Add(location);
               queryManager.Add(locationList);
               queryManager.IsTriggered(locationList);
               XmlDocument xDoc = queryManager.GetResults(locationList);
               DataSet resultSet = new DataSet();
               resultSet.ReadXml(new XmlNodeReader(xDoc));
               FillResultsGrid(resultSet.Tables[1]);
          }

          private void FillResultsGrid(DataTable resultTable)
          {
               //Instantiate the DataGrid
               resultsGrid = new DataGrid();
               //Set the DataSource
               resultsGrid.DataSource = resultTable;
               //Bind the data to the DataGrid
               resultsGrid.DataBind();
               //Add the DataGrid to the controls
               Controls.Add(resultsGrid);
          }
      }
}

See Also

Tasks

Walkthrough: Creating a Basic Search Web Part Using the Federation Object Model

Reference

Microsoft.Office.Server.Search.Query

Concepts

Enterprise Search Query Architecture

Other Resources

Search Federated Object Model Overview