smithsson68@gmail.com
Jul 12, 2011
  3864
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024