Orchard CMS 1.5.1 – Filtering Content by Taxonomies


Filtering contents by Taxonomies or categories in Orchard can be a challenge. I’ve found articles talking about cycling through taxonomies in a specific Content Type, but not one to get a list of Content Items for a custom filter. The reason I couldn’t use other methods is that I’m creating a custom search based on several other criteria, including a text field entered by the user.

Taxonomy terms are of type System.Linq.Enumerable.WhereSelectEnumerableIterator<Contrib.Taxonomies.Models.TermContentItem,Contrib.Taxonomies.Models.TermPart> which is difficult to work with. So after some trial and error, I found a way to do this.

In this example, I have a Content Type named Product. Inside the Product Content Type, I have a Product Part which contains the Brand taxonomy.

The method below takes in a brand name, and returns a IEnumerable<dynamic&gt list of Product Content Type with the brand name specified by the argument

private readonly IOrchardServices _orchardServices;

private IEnumerable&lt;dynamic&gt; GetFilteredItemsByTaxonomy(string taxonomyTerm)
{
    // Filtering for only product Content Type
    //   Using the dynamic type to access the taxonomy data directy
    IEnumerable&lt;dynamic&gt; products = _contentManager.Query(VersionOptions.Published
        , new string[] { &quot;Product&quot; }).List();

    // Filtering the products by brand where it contains the taxonomyTerm
    return products.Where(x =&gt;
      ((IEnumerable&lt;Contrib.Taxonomies.Models.TermPart&gt;)x.ProductPart.Brand.Terms.Value)
        .Select(y =&gt; y.Name).Contains(taxonomyTerm));
}

I would like to mention that PluralSight has a great Orchard Tutorial on Orchard modules.

About Antonio Zugno
Web Developer - Experienced with HTML, C#, VB, JavaScript, jQuery, MS SQL, XML, WordPress, PHP, and Adobe Flash.

One Response to Orchard CMS 1.5.1 – Filtering Content by Taxonomies

  1. Denis Besic says:

    How do you solve problem with performance? When you call List() you will get all items from database.