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

Forums / Developing with Sitefinity / Creating a custom search for news items

Creating a custom search for news items

8 posts, 0 answered
  1. Chris Warner
    Chris Warner avatar
    12 posts
    Registered:
    12 Oct 2009
    14 Oct 2009
    Link to this post
    Hi,

    I am a new user to Sitefinity and I`m trying to get my head around how things are done using this frame work!

    I want to write a control that allows me to filter news content by metadata/tags

    as an example I might have checkboxes that allow the user to filter out certain items etc.

    Is this something that would require a custom control? and if so how do I go about writing this control?

    Many thanks in advance
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    14 Oct 2009
    Link to this post
    Hi Chris Warner,

    The NewsView control supports filtering. You can check it out under the Advanced tab of the control >> Filters >> FilterExpression property. There is a textbox that you can use to enter your expression.

    Example:

    Publication_Date <= "#now" AND Expiration_Date > "#now"

    Another option is extending the control by crating a new section in the base tab from where to filter the items. There you can get all metakeys bound to a RadComboBox control and choose them.

    sample code:

    var manager = new ContentManager();
    var list = new List<MetaInfo>();
    var combo = new RadComboBox();
    Controls.Add(combo);
    foreach (MetaInfo info in manager.Provider.MetaKeys.Values)
    {
     
        list.Add(info);
        combo.DataSource = list;
        combo.DataTextField = "Caption";
        combo.DataBind();
         
    }

    But this will make the task complicated because you should predict all possible statements.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Chris Warner
    Chris Warner avatar
    12 posts
    Registered:
    12 Oct 2009
    14 Oct 2009
    Link to this post
    Many thanks for your reply,

    I understand that the newsview has a filter which would do this fine for me, however I want to allow the user to choose which filters are active...Can i do this without creating a control to provide the "selecting filter" functionality?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    14 Oct 2009
    Link to this post
    Hello Chris Warner,

    There are two options, both require extending the NewsView control.

    1. Create a property of type enum that will allow you to choose predefined values. Then set the value of the enum to the filter.

    2. Create a custom designer for your control and in the basic tab add  RadComboBox or another control which you should populate with predefined filters. Then set  the selectedValue to the filter.

    In both ways you need to create a custom control and override CreateChildcontrols and set your filter. You can just call this.FilterExpression = "YOUR PREDEFINED STATEMENT AS STRING";. Here you can use switch loop.

    protected override void CreateChildControls()
       {
     
           ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
           filterBuilder.AddFilter(new ContentFilterStatement());
           base.CreateChildControls();
       }


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Chris Warner
    Chris Warner avatar
    12 posts
    Registered:
    12 Oct 2009
    15 Oct 2009
    Link to this post
    Hi Ivan,

    Thanks for your help, I`m not sure if im going about this the correct way or not but the following HTML is the aspx page for the UserControl I am creating
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="SampleControl.ascx.cs" Inherits="UserControls_SampleControl" %> 
    <%@ Register Assembly="Telerik.News" Namespace="Telerik.News.WebControls" TagPrefix="tel" %> 
    <h3> 
    <asp:CheckBox runat="server" Text="Filter tc" ID="filterTCCheckBox" /> 
    </h3> 
    <p> 
        <asp:Button ID="Button1" runat="server" Text="Button" /> 
    </p> 
    <tel:NewsView runat="server" ID="newsView1"/> 
    and I want to change the filter on the news view on a postback, however due to the page lifecycle the NewsView is always one "Page post" behind i.e. the filter used in post back 1 will only show when another postback is initiated
    Here is the code behind
        string exp = string.Empty; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (IsPostBack) 
            { 
                setFilter(); 
            } 
        } 
     
     
     
     
        private void setFilter() 
        { 
            if (filterTCCheckBox.Checked) 
            { 
                exp = @"Tag.Name = ""tc"" "
                newsView1.FilterExpression = exp; 
            } 
            else 
            { 
     
                exp = @"Tag.Name = ""bob"" "
                newsView1.FilterExpression = exp; 
            } 
        } 
     
     

    Am i going about this the wrong way?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    15 Oct 2009
    Link to this post
    Hello Chris Warner,

    You can filter on button click event or force auto postback when the checkbox is checked.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Chris Warner
    Chris Warner avatar
    12 posts
    Registered:
    12 Oct 2009
    15 Oct 2009
    Link to this post
    Hi Ivan,

    I am filtering on the button postback, however the Filter already seems to have been applied to the NewsView, To be precise i am filtering on every postback, hence the code in page_load. However the filter is always one behind the selected option, as if the Newsview has rendered itself already

    Would you be able to import my code into a test project so that you can see what I mean?

    thanks

    chris
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    16 Oct 2009
    Link to this post
    Hello Chris Warner,

    Try using the following code:


    <asp:CheckBox ID="cb1" runat="server" Text="Filter1" AutoPostBack="true" />
    <asp:CheckBox ID="cb2" runat="server" Text="Filter2" AutoPostBack="true" />
    <news:NewsView runat="server" ID="NewsView1" />


    protected void Page_Load(object sender, EventArgs e)
     {
         if (IsPostBack)
         {
             FilterList();
             this.NewsView1.SingleItemUrl = this.NewsView1.SingleItemUrl;
         }
     
     
    public void FilterList()
     {
         ContentFilterBuilder filter = new ContentFilterBuilder(NewsView1);
         if (cb1.Checked)
         {
             filter.AddFilter(new ContentFilterStatement("Category", "MyCategory1", ContentFilter.Condition.Equal));
         }
         else
         {
             filter.ClearFilter();
         }
         if (cb2.Checked)
         {
             filter.AddFilter(new ContentFilterStatement("Category", "MyCategory2", ContentFilter.Condition.Equal));
         }
          
     }


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
8 posts, 0 answered