Posted by : Jebastin Thursday 7 August 2014

The following Generic Handler (C#) is used to unpublish the past/old Events automatically after it's expiration date in Umbraco.

Source Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. using umbraco.MacroEngines;
  7. using umbraco.cms.businesslogic.web;
  8. namespace KPMGAudit.Web.GenericHandlers
  9. {
  10.     /// <summary>
  11.     /// Summary description for UnpublishExpiredEvents
  12.     /// </summary>
  13.     public class UnpublishExpiredEvents : IHttpHandler
  14.     {
  15.         public void ProcessRequest(HttpContext context)
  16.         {
  17.             int EventsNodeId = Convert.ToInt32(ConfigurationManager.AppSettings["EventsNodeId"]);
  18.             string strEventStartDate = string.Empty, strEventEndDate = string.Empty;
  19.             context.Response.ContentType = "text/plain";
  20.             dynamic EventsNode = new DynamicNode().NodeById(EventsNodeId);
  21.             List<DynamicNode> eList = EventsNode.Children.Items;
  22.             foreach (var Event in eList)
  23.             {
  24.                 int CurrentEventNodeId = Convert.ToInt32(Event.Id);
  25.                 try
  26.                 {
  27.                     DateTime EventStartDate = Convert.ToDateTime(Event.GetProperty("eventStartDate").Value);
  28.                     strEventStartDate = EventStartDate.ToString("MM/dd/yyyy");
  29.                 }
  30.                 catch { strEventStartDate = string.Empty; }
  31.                 try
  32.                 {
  33.                     DateTime EventEndDate = Convert.ToDateTime(Event.GetProperty("eventEndDate").Value);
  34.                     strEventEndDate = EventEndDate.ToString("MM/dd/yyyy");
  35.                 }
  36.                 catch { strEventEndDate = string.Empty; }
  37.                 Document CurrentEventNode = new Document(CurrentEventNodeId);
  38.                 if (!string.IsNullOrEmpty(strEventEndDate))
  39.                 {
  40.                     if (Convert.ToDateTime(strEventEndDate) < DateTime.Now)
  41.                     {
  42.                         CurrentEventNode.UnPublish();
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     if (Convert.ToDateTime(strEventStartDate) < DateTime.Now)
  48.                     {
  49.                         CurrentEventNode.UnPublish();
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         public bool IsReusable
  55.         {
  56.             get
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.     }
  62. }
Or
  1. public void UnpublishNode(Document CurrentContentNode, DateTime ContentExpiryDate)
  2.         {
  3.             if (ContentExpiryDate != DateTime.MinValue)
  4.             {
  5.                 if (Convert.ToDateTime(ContentExpiryDate) < DateTime.Now)
  6.                 {
  7.                     CurrentContentNode.UnPublish();
  8.                 }
  9.             }
  10.         }

Configuration: (umbracoSettings.config)

  1.   <scheduledTasks>
  2.     <!-- add tasks that should be called with an interval (seconds) -->
  3.     <!--    <task log="true" alias="test60" interval="60" url="http://localhost/umbraco/test.aspx"/>-->
  4.     <task log="false" alias="ExpiredEventsUnpublishScheduler" interval="60" url="http://local.kpmg.com/GenericHandlers/UnpublishExpiredEvents.ashx"/>
  5.   </scheduledTasks>

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -