+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to Sort Search Result By Title, PublicationData, LastUpdated, etc..

How to Sort Search Result By Title, PublicationData, LastUpdated, etc..

4 posts, 0 answered
  1. Ruchira
    Ruchira avatar
    7 posts
    Registered:
    24 Apr 2013
    04 Jun 2013
    Link to this post
    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. 
  2. Omar
    Omar avatar
    8 posts
    Registered:
    24 Dec 2013
    15 Oct 2014 in reply to Ruchira
    Link to this post
    how I can do to make me order the search results based on the outcome golf products?
  3. Vassil Vassilev
     Vassil Vassilev avatar
    308 posts
    Registered:
    21 Jan 2015
    20 Oct 2014
    Link to this post
    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:
    Copy 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";
        }

    Template:
    Copy Code
    <%@ 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" />

    I hope you find this sample useful. You can use it as a starter to achieve your requirement.

    Regards,

    Vassil Vassilev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  4. Omar
    Omar avatar
    8 posts
    Registered:
    24 Dec 2013
    20 Oct 2014 in reply to Vassil Vassilev
    Link to this post

    Thank you .

    But my code is based on the following link

    http://www.sitefinity.com/blogs/boyan-barnevs-blog-/2012/11/06/customizing-sitefinity-searchresults---displaying-product-thumbnails-and-other-content-type-specific-data

     
    any idea as being able to order?

     

     

4 posts, 0 answered