Example: Filter dynamic content items by dynamic field
This example uses a Movie Collection module with the following hierarchical structure:
- The module is named Movie Collection
- The module has content type Movies. Movies holds information about different movies and has the following fields:
- Title - ShortText field
- Year - Number field
- ShotIn3D - Yes/No field
- Genre- Choicesfield
- Action
- Drama
- Comedy
- Tags- Classification field
- Actors- RelatedDatafield
- Poster- RelatedMediafield
- The module has content type Actor. The Actor content type has only one field Name that contains the name of the actor.
C#
using System;
using System.Linq;
using Telerik.Sitefinity.Data.Linq.Dynamic;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.RelatedData;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.SFTools.TemplateBuilder.HierarchicalDM
{
public partial class FilterDynamicContentItems
{
private void FilterDynamicContent()
{
// Get an instance of the dynamic module manager
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
// Resolve dynamic content item types
Type movieType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MovieCollection.Movie");
Type actorType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MovieCollection.Actor");
// Get collection of movies
var allMovies = dynamicModuleManager.GetDataItems(movieType);
// Get collection of actors
var allActors = dynamicModuleManager.GetDataItems(actorType);
// Filter movie collection by ShortText field
var moviesByTitle = allMovies.Where("Title = \"Some Title\"");
var moviesByTitleAlternative = allMovies.Where("Title.Contains(\"Some Title\")");
var moviesByTitleAndStatus = allMovies.Where("Title.Contains(\"Some Title\") && Status = \"Live\"");
// Filter movie collection by a Number field
var moviesFrom2003 = allMovies.Where("Year = 2003");
var moviesBefore2000 = allMovies.Where("Year < 2000");
// Filter movie collection by a YesNo field
var moviesIn3D = allMovies.Where("ShotIn3D = true");
var moviesNotIn3D = allMovies.Where("ShotIn3D = false");
// Filter movie collection by a Choices field
var dramaMovies = allMovies.Where("Genre = \"Drama\"");
var actionMovies = allMovies.Where("Genre = \"Action\"");
var comedyMovies = allMovies.Where("Genre = \"Comedy\"");
// Filter movie collection by Classification field
var taxonomyManager = TaxonomyManager.GetManager();
var tags = taxonomyManager.GetTaxonomies<FlatTaxonomy>().FirstOrDefault(x => x.Title == "Tags");
var tag = tags.Taxa.FirstOrDefault(x => x.Title == "Some movie tag");
var moviesByTag = allMovies.Where("Tags.Contains((" + tag.Id.ToString() + "))");
// Filter movie collection by RelatedData field
var actor = allActors.Where("Name.Contains(\"Actor Name\")").FirstOrDefault();
var moviesByActor = actor.GetRelatedParentItems(movieType.FullName, providerName, "Actors").OfType<DynamicContent>();
// Filter movie collection by RelatedMedia field
var image = LibrariesManager.GetManager().GetImages().FirstOrDefault();
var moviesByPoster = image.GetRelatedParentItems(movieType.FullName, providerName, "Poster").OfType<DynamicContent>();
}
}
}
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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your 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 ASP.NET Core and take advantage of its decoupled architecture and modern development model.