Although the SharePoint 2010 out-of-the-Box Search interface is already intuitive and easy to use for information workers, it is still possible to create your own search experience as a superuser. SharePoint Server 2010 includes a number of powerful Web Parts related to search that support a custom search experience for super users, including search for best bets, thin panels, search core results, related queries, and more. The following figure is a standard search class WebPart.
IT Pros or developers can configure the built-in search Web Parts to customize the search experience. As a developer, you can also extend these webpart to change the behavior of the built-in search webpart on the search results page. You only need to inherit and extend on an existing basis without creating a new WebPart.
In addition, we can use query logging when customizing Search WebPart, and access the search service at any time through the query object model.
Example: Extended search Core Results Web Part
Let's create a new search WebPart with Visual Studio 2010. The WebPart in this example inherits from CoreResultsWebPart and displays the data from a custom source. The standard search core results WebPart includes a constructor and two methods, which we will modify in this example.
The first step is to create a new WebPart class. Create a new WebPart project and inherit from the CoreResultsWebPart class. Overwrite CreateChildControls to add any controls required by the interface, and then overwrite the CreateDataSource. This is the entrance to our operation query. In the overwrite, we will create an instance of the custom data source class that is created later.
class MSDNSample : CoreResultsWebPart
{
public MSDNSample()
{
//默认构造器:支持为了序列化所以必须存在
}
protected override void CreateChildControls()
{
base.CreateChildControls();
//在此处添加界面上用到的控件
}
protected override void CreateDataSource()
{
//base.CreateDataSource();
this.DataSource = new MyCoreResultsDataSource(this);
}
The second step is to create a new class that inherits from the Coreresultsdatasource class. In CreateDataSource's overwrite, the DataSource property is set to the class. In the Coreresultsdatasource constructor, create an instance of the custom data source view class that you created later. At this point, there is no need to overwrite any other content.
public class MyCoreResultsDataSource : CoreResultsDatasource
{
public MyCoreResultsDataSource(CoreResultsWebPart ParentWebpart)
: base(ParentWebpart)
{
//如果在这里我们需要引用该WebPart的属性或方法
//那么可以通过 ParentWebPart 参数获取
//创建将用于此数据源的视图
this.VIEw = new MyCoreResultsDataSourceView(this, "MyCoreResults");
}
}