Filtering News etc. by category
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.
Thanks!
Hi,
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 get return "~/CustomControls/TitlesDatesListView.ascx"; set base.LayoutTemplatePath = value; public virtual RadComboBox CategoriesCombo get return this.Container.GetControl<RadComboBox>("categoriesCombo", false); public virtual Button FilterButton get 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) .Distinct() .Select(t => new taxonId = t.TaxonId, taxonTitle = taxonomyManager.GetTaxon<HierarchicalTaxon>(t.TaxonId).GetString("Title"), ).ToList(); this.CategoriesCombo.DataTextField = "taxonTitle"; this.CategoriesCombo.DataValueField = "taxonId"; this.CategoriesCombo.DataSource = newsCategories; this.CategoriesCombo.DataBind(); 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); base.OnPreRender(e); <%@ 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"> <LayoutTemplate> <sf:ContentBrowseAndEditToolbar ID="MainBrowseAndEditToolbar" runat="server" Mode="Add"></sf:ContentBrowseAndEditToolbar> <ul class="sfnewsList sfnewsListTitleDate"> <asp:PlaceHolder ID="ItemsContainer" runat="server" /> </ul> </LayoutTemplate> <ItemTemplate> <li class="sfnewsListItem"> <h2 class="sfnewsTitle"> <sf:DetailsViewHyperLink TextDataField="Title" ToolTipDataField="Description" runat="server" /> </h2> <div class="sfnewsMetaInfo"> <sf:FieldListView ID="PublicationDate" runat="server" Format="PublicationDate.ToLocal():MMM dd, yyyy" /> <sf:CommentsBox ID="itemCommentsLink" runat="server" CssClass="sfnewsCommentsCount"/> </div> <sf:ContentBrowseAndEditToolbar ID="BrowseAndEditToolbar" runat="server" Mode="Edit,Delete,Unpublish"></sf:ContentBrowseAndEditToolbar> </li> </ItemTemplate></telerik:RadListView><sf:Pager id="pager" runat="server"></sf:Pager>