Exemple de code : code de composant WebPart personnalisé de recherche fédérée
Dernière modification : lundi 19 avril 2010
S’applique à : SharePoint Server 2010
L’exemple suivant est le code complet du composant WebPart personnalisé de recherche fédérée, décrit dans Procédure pas à pas : création d’un composant Web Part de recherche de base à l’aide du modèle objet de fédération.
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);
}
}
}
Voir aussi
Tâches
Référence
Microsoft.Office.Server.Search.Query
Concepts
Architecture d’interrogation de la recherche de contenu d’entreprise