Custom filtering of MVC Events widget

MVC events widget contains various predefined options to filter the list of displayed events. Although you can adjust almost every setting in the designer, you may want to extend the business logic behind some of these default filtering options. This tutorial will demonstrate how to extend a Eventswidget.

Perform the following:

  1. Create a new class that represents you new business logic for the Events widget.
    Create a CustomEventModel class that inherits from the default EventModel.
    In this example, you additionally filter all events with title that contains Some Title text:
    C#
    using System;
    using Telerik.Sitefinity.Frontend.Events.Mvc.Models;
    
    namespace SitefinityWebApp
    {
        public class CustomEventModel : EventModel
        {
            protected override string CompileFilterExpression()
            {
                var filterExpression = base.CompileFilterExpression();
    
                if (!filterExpression.IsNullOrEmpty())
                    filterExpression += " AND (Title.Contains(\"Some Title\"))";
                else
                    filterExpression = "(Title.Contains(\"Some Title\"))";
    
                return filterExpression;
            }
        }
    }
  2. Replace the original implementation of the Events widget model with the new model. 
    You do this using the Bootstrapper.Bootstrapped event.
    Place the following code in your Global.asax file:
    C#
    using System;
    using Telerik.Sitefinity.Abstractions;
    using Telerik.Sitefinity.Frontend;
    using Telerik.Sitefinity.Frontend.Events.Mvc.Models;
    
    namespace SitefinityWebApp
    {
        public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                Bootstrapper.Bootstrapped += this.Bootstrapper_Bootstrapped;
            }
    
            private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
            {
                FrontendModule.Current.DependencyResolver.Rebind<IEventModel>().To<CustomEventModel>();
            }
        }
    }

RESULT: You changed the default filtering of events due to the many virtual implementations of all controller methods and model methods.

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.
New to Sitefinity?