Filter dynamic content items by dynamic field

When working with dynamic content items with dynamic fields, you may need to display only a portion of these content items in the frontend. With Sitefinity CMS, you can filter programmatically the content items by their dynamic fields.

For example, you have a Movie collection dynamic content item and you may need to filter this collection to show only movies from a particular genre, designated by a dynamic field. For more details and sample code for this example, see Example: Filter dynamic content items by dynamic field.

This article describes the concept and procedure how to filter dynamic content items by a dynamic field.

Check whether field exists

Before filtering items by a field, we recommend to check if the dynamic field exists and is not removed, for example. The following code demonstrates how to do this:

Filter dynamic content items

Before you begin filtering your dynamic content items, you need to get an instance of the DynamicModuleManager class.

Next, you need to get the type of the dynamic content item by using the TypeResolutionService class.

To filter dynamic content items, you use IQueryable extension methods - the Where() method and pass a string predicate. To do this, you must include the following namespace in your code:

  • System.Linq;
  • Telerik.Sitefinity.Data.Linq.Dynamic;

By using the Where() extension method, execute complex string predicates.

In addition, you can filter dynamic content items by the following dynamic field types:

  • Number
    The string predicate for the extension method must include the name of the Number field and a decimal value.
  • Yes/No
    The string predicate for the extension method must include the name of the 
    Yes/No field and a Boolean value.
  • Choices
    The string predicate for the extension method must include the name of the 
    Choices field and the Choices value.
  • Classification
    The string predicate for the extension method must include the name of the 
    Classification field and the ID of the taxon.
  • Related data
    To filter dynamic content items by a related data field for another dynamic content item, you use the GetRelatedParentItems extension method.
  • Related media
    To filter dynamic content items by a related media field (for example, an image), you use the GetRelatedParentItems extension method.     

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?