How to Sort Search Result By Title, PublicationData, LastUpdated, etc..
I just want to sort search result by Title, PublicationData, LastUpdated etc.. Please see the attached image for more details. It has separate drop-down for select search field. I just want to sort the search result when selecting an item from the drop-down.
how I can do to make me order the search results based on the outcome golf products?
Hello,
The approach for achieving the desired result is to make a custom SearchResults widget inheriting from the default one and using its template, which we will customize to add the widgets to display the required data. I have made a sample for you, where I use the base logic for the searching and only add the logic for displaying the desired data and sorting. Basically, you could get the fields of the result items and filter them, or use an item OriginalContentId to query it. The easiest way to get the items count is to get the number of items bound to the repeater, since there is no property set for this and the variable is 'hidden' in the base InitializeControls.
Result Video.
Code:
public
class
CustomSearch : SearchResults
public
override
string
LayoutTemplatePath
get
return
CustomSearch.CustomSearchLayoutTemplatePath;
set
base
.LayoutTemplatePath = value;
public
Label TitleLabel
get
return
this
.Container.GetControl<Label>(
"Title"
,
true
);
public
Label PagesLabel
get
return
this
.Container.GetControl<Label>(
"Pages"
,
true
);
public
Button Sort
get
return
this
.Container.GetControl<Button>(
"Sort"
,
true
);
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
base
.InitializeControls(container);
this
.Sort.Click += Sort_Click;
if
(!
string
.IsNullOrWhiteSpace(
this
.Query))
this
.TitleLabel.Text =
string
.Format(
"You searched for '0'"
,
this
.Query);
var data =
this
.ResultsList.DataSource
as
IEnumerable<
object
>;
if
(data !=
null
)
var count = data.Count();
this
.PagesLabel.Text =
string
.Format(
"0 Results Found"
, count.ToString());
if
(count > 0)
this
.Sort.Visible =
true
;
void
Sort_Click(
object
sender, EventArgs e)
var data =
this
.ResultsList.DataSource
as
IEnumerable<IDocument>;
data = data.OrderByDescending(t => t.GetValue(
"Title"
));
this
.ResultsList.DataSource = data;
this
.ResultsList.DataBind();
private
static
string
CustomSearchLayoutTemplatePath =
"~/Widgets/CustomSearch/CustomSearchTemplate.ascx"
;
<%@ Control Language="C#" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI" TagPrefix="sitefinity" %>
<%@ Register Assembly="Telerik.Sitefinity.Search.Impl" Namespace="Telerik.Sitefinity.Services.Search.Web.UI.Public" TagPrefix="sfSearch" %>
<
sfSearch:SearchBox
ID
=
"topSearchBox"
runat
=
"server"
/>
<
asp:Label
ID
=
"Title"
runat
=
"server"
Text
=
""
></
asp:Label
>
<
asp:Label
ID
=
"Pages"
runat
=
"server"
Text
=
""
></
asp:Label
>
<
asp:Button
ID
=
"Sort"
runat
=
"server"
Text
=
"Sort"
Visible
=
"false"
/>
<
asp:Repeater
ID
=
"resultsList"
runat
=
"server"
>
<
HeaderTemplate
>
<
dl
class
=
"sfsearchResultsWrp sfsearchReultTitleSnippetUrl"
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
dt
class
=
"sfsearchResultTitle"
><
a
id
=
"A1"
runat
=
"server"
href='<%# Eval("Link")%>'><%# Eval("Title") %></
a
></
dt
>
<
dd
class
=
"sfsearchResultSnippet"
><%# Eval("Summary")%></
dd
>
<
dd
class
=
"sfsearchResultUrl"
><
a
id
=
"A2"
runat
=
"server"
href='<%# Eval("Link")%>'><%# Eval("Link")%></
a
></
dd
>
<
dd
class
=
"sfsearchResultHighLighter"
><%# Eval("HighLighterResult")%></
dd
>
</
ItemTemplate
>
<
FooterTemplate
>
</
dl
>
</
FooterTemplate
>
</
asp:Repeater
>
<
sitefinity:Pager
ID
=
"pager"
runat
=
"server"
/>
<
sfSearch:SearchBox
ID
=
"bottomSearchBox"
runat
=
"server"
/>
Thank you .
But my code is based on the following link
any idea as being able to order?