Restricting Sitecore Content to display only in certain times

A common use case you hear from content editors is to add the ability to ‘auto-publish’ content so that they can schedule the introduction of new content to the cms. On some of these occasions, this can be achieved by applying a window to content rather than attempting to publish it in a given window.

It is worth noting – to make this a more complete solution, additional work may be required to ensure links etc do not show, this has to be treated more specifically.

Screen Shot 2017-06-02 at 11.51.30


using System;
using Sitecore;
using Sitecore.Pipelines.HttpRequest;

namespace Website.Pipelines
{
   ///
<summary>
   /// Pipeline to modify sitecore to prevent item resolution if restrictions are set
   /// </summary>

   public class RestrictedItemResolver : ItemResolver
   {
      public override void Process(HttpRequestArgs args)
      {
         base.Process(args);
         ApplyItemRestrictions();
      }

      protected virtual void ApplyItemRestrictions()
      {
         if (!Context.Database.Name.Equals("web", StringComparison.OrdinalIgnoreCase))
         {
            return;
         }

         ApplyEarliestViewableTime();
         ApplyLatestViewableTime();
      }

      protected virtual void ApplyEarliestViewableTime()
      {
         if (Context.Item == null)
         {
            return;
         }

         string fieldValue = Context.Item["Viewable From"];
         if (string.IsNullOrEmpty(fieldValue))
         {
            return;
         }

         DateTime date = DateUtil.IsoDateToDateTime(fieldValue);
         if (DateTime.UtcNow &lt; date)
         {
            Context.Item = null;
         }
      }

      protected virtual void ApplyLatestViewableTime()
      {
         if (Context.Item == null)
         {
            return;
         }
 
         string fieldValue = Context.Item["Viewable To"];
         if (string.IsNullOrEmpty(fieldValue))
         {
            return;
         }
 
         DateTime date = DateUtil.IsoDateToDateTime(fieldValue);
         if (DateTime.UtcNow &gt; date)
         {
            Context.Item = null;
         }
      }
   }
}
Advertisements

4 thoughts on “Restricting Sitecore Content to display only in certain times

  1. How do you handle de-caching when items appear/disappear? Also looks like the snippet is missing `ApplyLatestViewableTime`

    • However, for the most part, since solution this prevents item resolution, then from a Sitecore perspective, I believe the 404 structures should take care of this. It’s been a while, but I recall the caching occurs in the rendering based pipelines, so in this as a concept, the page either displays or doesn’t.

      If you are talking about CDN type caching, very little that can be done there if the content has an end date as it will not disappear, in other cases, it should be ok as it continues.

      As with all solutions, intelligence has to be applied around caching, so this solution may not be useful for all.

      PS: Thanks for the snippet notification, will check that out (#readyourcodebeforeyoupublish)

      • We were toying with an idea very similar to this before. The problem we hit was at the Sitecore html cache layer.

        Say you have a results grid which shows all the items in a folder – your new item is ‘magically’ allowed to then show in this folder at a point in time, the html caches have no concept of this change so would never render the new item until a sitecore publish happens.

      • Yeah, that is the same for many other conditions mind such as user data in a control etc or search data, so your component would have to remain uncached in that scenario. For the use case this was built for, that wasn’t an issue to the client (simple news articles)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s