Customize search Results WebPart sharepoint 2010

You can either add a custom property to be shown in your results webpart(See example here or you can create your own results webpart that inherit from CoreResultsWebPart class and displays data from a custom source. The standard CoreResultsWebPart part includes a constructor and then two methods that we will modify in this example. Please note that this is a MSDN example.

The first step is to create a new WebPart class. Create a new web part project that inherits from the CoreResultsWebPart class. Override CreateChildControls to add any controls necessary for your interface, and then override CreateDataSource . In the override, you will create an instance of a custom datasource class you will build.

class Sample : CoreResultsWebPart
{
public Sample()
{//default constructor; }
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 CoreResultsDatasource class. In the override for CreateDataSource , set the DataSource property to a new class that inherits from
CoreResultsDataSource . In the CoreResultsDataSource constructor, create an instance of a custom datasource view class you will build. No other overrides are necessary.

public class MyCoreResultsDataSource : CoreResultsDatasource
{
public MyCoreResultsDataSource(CoreResultsWebPart ParentWebpart) : base(ParentWebpart)
{
//to reference the properties or methods of the web part
//use the ParentWebPart parameter
//create the View that will be used with this datasource
this.View = new MyCoreResultsDataSourceView(this,”MyCoreResults”);
}}
The third step is to create a new CoreResultsDatasourceView class. Set the View property for your CoreResultsDatasource to a new class that inherits from CoreResultsDatasourceView . In the CoreResultsDatasourceView constructor, get a reference to the CoreResultsDatasource so that you can refer back to the web part. Then, set the QueryManager property to the shared query manager used in the page.

public class MyCoreResultsDataSourceView : CoreResultsDatasourceView
{
public MyCoreResultsDataSourceView(SearchResultsBaseDatasource DataSourceOwner, string ViewName) : base(DataSourceOwner, ViewName)
{
//make sure we have a value for the datasource
if (DataSourceOwner == null)
{
throw new ArgumentNullException(“DataSourceOwner”);
}
//get a typed reference to our datasource
MyCoreResultsDataSource ds = this.DataSourceOwner as MyCoreResultsDataSource;
//configure the query manager for this View
this.QueryManager = SharedQueryManager.GetInstance (ds.ParentWebpart.Page).QueryManager;
}

You now have a functional custom web part displaying data from your custom source.

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com