More in this section

Forums / Bugs & Issues / Filtering News etc. by category

Filtering News etc. by category

3 posts, 0 answered
  1. Compton
    Compton avatar
    18 posts
    02 Feb 2012
    15 Feb 2012
    Link to this post
    So far as I can tell, there are only two ways to filter content by category: either by setting a fixed category in the module properties, or by dropping a categories widget onto the module's page.   What I'd like to be able to set a session variable and have the content filter based on that, without the user having to select the category as they navigate from page to page.  In my specific application, I want users to be able to pick a city at the beginning of their session and then filter all of my content based on that city.

    One approach I had been considering is creating a new categories widget, based on the existing Categories widget, and then have it set its state based on a session variable.   There is an article that describes how to customize Sitefinity widgets, I tried following these instructions but I could not even find the Categories widget under Settings/System/ApplicationModules.

    Any suggestions would be greatly appreciated.

  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    28 Aug 2017
    20 Feb 2012
    Link to this post

    It's possible to achieve the desired functionality with not that much modification, and preserving the default look and feel of the control. You'll need to modify the default ListView as well as the Template.Please consider the following sample, based on the NewsView built in control.
    1) We need to override the list mode of the control. This will allow us to add additional logic and control. To do so create a custom control which inherits from MasterListView:
    using System;
    using System.Linq;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.News.Web.UI;
    using Telerik.Sitefinity.News.Model;
    using Telerik.Sitefinity.Taxonomies;
    using Telerik.Sitefinity.Taxonomies.Model;
    using Telerik.Web.UI;
    namespace SitefinityWebApp.CustomControls
        public class CustomMasterListView: MasterListView
            public override string LayoutTemplatePath
                    return "~/CustomControls/TitlesDatesListView.ascx";
                    base.LayoutTemplatePath = value;
            public virtual RadComboBox CategoriesCombo
                    return this.Container.GetControl<RadComboBox>("categoriesCombo", false);
            public virtual Button FilterButton
                    return this.Container.GetControl<Button>("filterBtn", false);
            protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container, Telerik.Sitefinity.Web.UI.ContentUI.Contracts.IContentViewDefinition definition)
                //create a categories dropdown.
                TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
                var newsCategories = taxonomyManager.GetStatistics()
                                        .Where(tS => tS.DataItemType == "Telerik.Sitefinity.News.Model.NewsItem" && tS.TaxonomyId == TaxonomyManager.CategoriesTaxonomyId)
                                        .Where(tS => tS.StatisticType == ContentLifecycleStatus.Live && tS.MarkedItemsCount > 0)
                                        .Select(t => new
                                            taxonId = t.TaxonId,
                                            taxonTitle = taxonomyManager.GetTaxon<HierarchicalTaxon>(t.TaxonId).GetString("Title"),
                this.CategoriesCombo.DataTextField = "taxonTitle";
                this.CategoriesCombo.DataValueField = "taxonId";
                this.CategoriesCombo.DataSource = newsCategories;
                RadComboBoxItem emptyItem = new RadComboBoxItem("Choose category");
                this.CategoriesCombo.Items.Insert(0, emptyItem);           
            protected override IQueryable<Telerik.Sitefinity.News.Model.NewsItem> GetItemsList(ref int? totalCount)
                //check if a category to filter by is selected and add it to the filter expression
                if (this.CategoriesCombo.SelectedIndex > 0)
                    this.FilterExpression += string.Format(" AND Category.Contains(({0}))", this.CategoriesCombo.SelectedValue.ToString());
                //populate items list
                return base.GetItemsList(ref totalCount);
            protected override void OnPreRender(EventArgs e)
                int? totalCount = 0;
                IQueryable<NewsItem> query = this.GetItemsList(ref totalCount);
                this.InitializeListView(query, totalCount);

    2) Create a custom template that has the combobox and the button that causes filtration:
    <%@ Control Language="C#" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <telerik:RadComboBox runat="server" ID="categoriesCombo" Skin="Sitefinity"></telerik:RadComboBox>
    <asp:Button runat="server" ID="filterBtn" Text="Submit" />
    <telerik:RadListView ID="NewsList" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
            <sf:ContentBrowseAndEditToolbar ID="MainBrowseAndEditToolbar" runat="server" Mode="Add"></sf:ContentBrowseAndEditToolbar>
            <ul class="sfnewsList sfnewsListTitleDate">
                <asp:PlaceHolder ID="ItemsContainer" runat="server" />
            <li class="sfnewsListItem">
                <h2 class="sfnewsTitle">
                    <sf:DetailsViewHyperLink TextDataField="Title" ToolTipDataField="Description" runat="server" />
                <div class="sfnewsMetaInfo">
                    <sf:FieldListView ID="PublicationDate" runat="server" Format="{PublicationDate.ToLocal():MMM dd, yyyy}" />
                    <sf:CommentsBox ID="itemCommentsLink" runat="server" CssClass="sfnewsCommentsCount"/>
                <sf:ContentBrowseAndEditToolbar ID="BrowseAndEditToolbar" runat="server" Mode="Edit,Delete,Unpublish"></sf:ContentBrowseAndEditToolbar>
    <sf:Pager id="pager" runat="server"></sf:Pager>

    3) Go to Administration -> Settings -> Advanced -> News -> Controls -> NewsFrontend -> Views and replace the ViewType of the list view with the custom one.

    4) Enable view state on the page.

    Once you place the widget on a page you'll notice the newly appeared dropdown which should be populated by the categories you have assigned to your content item. Selecting any of them and clicking the button will
    automatically adjust the filter expression to match the desired category. You can modify the provided sample slightly to work with the desired Content type and implement the specific filtering functionality- the principle is the same.

    Boyan Barnev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. Developer
    Developer avatar
    4 posts
    04 May 2012
    23 May 2012
    Link to this post
    Hello Boyan,
    Thanks for sharing the code, after implementing this code I am getting the news according to the applied filter but I am not able to navigate on detail page of news. So, when I click on news heading same page renders again. Any clue how can I solve this issue?

    Thanks in advance
3 posts, 0 answered