Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

smithsson68@gmail.com
Jul 12, 2011
  3834
(3 votes)

Time Period Visitor Group Criterion

I saw a requirement from a customer who wanted to be able to set start and stop publish dates for different parts of a page.

This of course can be done by creating and maintaining several page versions but sounds like quite a clumsy solution to me.

With that said, please say hello to TimePeriodCriterion. This works by having a start date and time and an end date and time. The criterion only matches when the current date and time are within the time range specified.

namespace CriteriaPack.TimePeriodCriterion
{
    [VisitorGroupCriterion(
      DisplayName = "Time Period", 
      Category = "Time and Place Criteria", 
      LanguagePath = "/shell/cms/visitorgroups/criteria/timeperiod")]
    public class TimePeriodCriterion : CriterionBase<TimePeriodCriterionModel>
    {
        public override bool IsMatch(System.Security.Principal.IPrincipal principal, HttpContextBase httpContext)
        {
            return DateTime.Now >= this.RangeStart && DateTime.Now <= RangeEnd;
        }

        protected DateTime RangeStart
        {
            get
            {
                return Model.StartDate.Add(Model.StartTime.TimeOfDay);
            }
        }

        protected DateTime RangeEnd
        {
            get
            {
                return Model.EndDate.Add(Model.EndTime.TimeOfDay);
            }
        }
    }

    public class TimePeriodCriterionModel : CriterionModelBase
    {
        [DojoWidget(WidgetType="dijit.form.DateTextBox")]
        [Required]
        public DateTime StartDate { get; set; }

        [DojoWidget(WidgetType = "dijit.form.TimeTextBox")]
        [Required]
        public DateTime StartTime { get; set; }

        [DojoWidget(WidgetType = "dijit.form.DateTextBox")]
        [Required]
        public DateTime EndDate { get; set; }

        [DojoWidget(WidgetType = "dijit.form.TimeTextBox")]
        [Required]
        public DateTime EndTime { get; set; }

        public override ICriterionModel Copy()
        {
            // We can use the ShallowCopy method as this class
            // does not have any members that need to be deep copied
            return base.ShallowCopy();
        }
    }
}

A couple of the advantages of implementing start/stop publish as a criterion are that it can of course be combined with other criteria in a Visitor Group and you can also specify fallback content, i.e. something else to display on the page when the end time and date has been passed.

For example, competition information on a start page:

Create an EPiServer Visitor Group with the Time Period criterion specifying the dates the July competition is open for entry:

image

Information can now be added about the competition to the start page in a personalized content block selecting the ‘July Competition Entry Period’ Visitor Group created with the Time Period criterion.

image

The information about entering the competition will now only be shown between the dates specified in the criterion, otherwise the fallback content will be specified.

A NuGet package called TimePeriodCriterion containing just the re-distributable components (as with all CriterionPack NuGet packages) can be downloaded from nuget.episerver.com, alternatively you can get the source code from CodePlex.

Jul 12, 2011

Comments

Jul 12, 2011 01:59 PM

A simple yet useful criteria. Nice work!

Please login to comment.
Latest blogs
Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog