Core Results WebPart is a famous web part that allows you to query data, filter it and display it, quite nicely. In Sharepoint 2007 we were not able to do anything with that web part, but in Sharepoint 2010 Microsoft provides us a mecahanism to create your own web parts with the CoreResultsWebPart class.
What you do, basically is override the method:
protected override void ConfigureDataSourceProperties()
From there you can collects the CoreResultsDatasource and sets the sort order you are interested.
I personally didn’t know where to start with this, as Microsoft doesn’t provide plenty of information about it, but after reading an article of Bart-Jan Hoeijmakers I understood the logic behind. I have copied/pasted his code, improved it a little bit and put it nice for you.
What you do it is create a web part, but instead of use WebPart as base class your use CoreResultsWebPart and override the main method.
The positive thing about this issue, it is the fact you can display extra properties in edit mode to refine the search even more.
I am going to do a Step by Step project and at the end of the article you will be able to find the source code to download (but read it first!).
1- Go to Visual Studio 2010->New Project->Sharepoint->2010->Empty Sharepoint Project. Call it netsourcecodeCoreResultsWebPart.
2- Go to your project-> Right Click->Add new-> Web Part. Call it CoreSearchWebPart.
3- Add the Microsoft.Office.Server.Search in the references folder.
4- Go to CoreSearchWebPart.cs, remove all the code and paste the one below.
using System;using System.ComponentModel;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using Microsoft.SharePoint;using Microsoft.SharePoint.WebControls;using Microsoft.Office.Server.Search.WebControls;namespace netsourcecodeCoreResultsWebPart.CoreSearchWebPart{[ToolboxItemAttribute(false)]public class CoreSearchWebPart : CoreResultsWebPart{[Personalizable(PersonalizationScope.Shared)][WebBrowsable(true)][WebDescription("Sort by this managed property")][Category("NetSourceCode")]public string OrderByProperty { get; set; }[Personalizable(PersonalizationScope.Shared)][WebBrowsable(true)][WebDescription("Sort direction")][Category("NetSourceCode")]public Microsoft.Office.Server.Search.Query.SortDirection SortDirection { get; set; }/// <summary>/// Override. First runs the base code, than collects the CoreResultsDatasource and/// sets the SortOrder property./// </summary>protected override void ConfigureDataSourceProperties(){// only do stuff when search results are visibleif (this.ShowSearchResults){// run the base codebase.ConfigureDataSourceProperties();try{// if OrderByProperty is not set, use default behaviourif (!string.IsNullOrEmpty(OrderByProperty)){// get the datasource and change the sortorderCoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource;dataSource.SortOrder.Clear();dataSource.SortOrder.Add(OrderByProperty, SortDirection);}}catch (Exception ex){throw ex;}}}//protected override void CreateChildControls()//{//}}}
5- Deploy the solution!
6- Go to your site, Site Actions->Edit Page->Page->Insert and go to Custom you should able to see the CoreSearchWebPart, click on Add.
7- Edit the Web part… this should be the result.
Conclusion: CoreResultsWebPart Specifies the user interface (UI) control that displays the results for a search query in the SharePoint Enterprise Search UI, with the possibility of showing anything you want.
3 comments:
hollister clothing
coach outlet store online
cheap rolex watches
tiffany and co jewelry
cheap ray ban sunglasses
replica rolex watches
coach outlet store online
true religion
prada handbags
discount ray ban sunglasses
2016.12.17xukaimin
www0728
true religion jeans
kate spade
jordan shoes
off white outlet
christian louboutin sale
canada goose jackets
ugg boots uk
ugg boots clearance
prada outlet
off white shoes
golden goose outlet
curry shoes
hermes
golden goose sneakers
golden goose
palm angels
jordan 1 mid
supreme clothing
jordan shoes
golden goose
Post a Comment