Filter by category or tag

PREREQUISITES: To be able to filter a widget by a taxonomy, drop the Categories widget on the page and assign some items to some categories.

Create the controller

If your controller has an action named ListByTaxon with first parameter Telerik.Sitefinity.Taxonomies.Model.ITaxon then Sitefinity CMS invokes this action, if a category or a tag matches the URL segment after the page URL.

You can optionally have a second parameter of type int? that you can use for paging.

Sitefinity CMS first tries to match a category, then it falls back to tags. When an item matches, the action is invoked with the matching taxon.

The following sample implementation of the controller filters NewsItems:

C#
using SitefinityWebApp.Mvc.Models;
using System.ComponentModel;
using System.Web.Mvc;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Mvc;
using Telerik.Sitefinity.Taxonomies.Model;
using System.Linq;
using Telerik.Sitefinity.Model;
using System.Collections.Generic;
using System;

namespace SitefinityWebApp.Mvc.Controllers
{
   [ControllerToolboxItem(Name = "TaxonTestWidget_MVC", Title = "TaxonTestWidget", SectionName = "CustomWidgets")]
   public class TaxonTestWidgetController : Controller
   {
       private TaxonTestWidgetModel model;

       [TypeConverter(typeof(ExpandableObjectConverter))]
       public virtual TaxonTestWidgetModel Model
       {
           get
           {
               if (this.model == null)
               {
                   this.model = new TaxonTestWidgetModel();
               }

               return this.model;
           }
       }

       // GET: TaxonTestWidget
       public ActionResult Index()
       {
           var news = NewsManager
               .GetManager()
               .GetNewsItems()
               .Where(x => x.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
               .ToList();
           this.model.NewsItems = news;

           return View(this.model);
       }

       public ActionResult ListByTaxon(ITaxon taxonFilter, int? page)
       {
           var manager = NewsManager.GetManager();

           string fieldName;
           if (taxonFilter.Taxonomy.Name == "Categories")
               fieldName = taxonFilter.Taxonomy.TaxonName;
           else
               fieldName = taxonFilter.Taxonomy.Name;

           var items = manager.GetNewsItems()
               .Where(n => n.GetValue<IList<Guid>>(fieldName)
               .Contains(taxonFilter.Id) && n.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
               .ToList();

           this.model.NewsItems = items;

           return View("Index", this.model);
       }

   }
}

Create the model

Use the following code:

C#
using System.Collections.Generic;
using Telerik.Sitefinity.News.Model;

namespace SitefinityWebApp.Mvc.Models
{
   public class TaxonTestWidgetModel
   {
       public List<NewsItem> NewsItems { get; internal set; }
   }
}

Create the view

Use the following code:

HTML+Razor
@model SitefinityWebApp.Mvc.Models.TaxonTestWidgetModel

<div class="row">@foreach (var newsItem in Model.NewsItems)
   {
       <div class="col-md-6">
           <h2>@newsItem.Title</h2>
           <p><a href="@newsItem.ItemDefaultUrl">Read More</a></p>
       </div>
   }
</div>
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.