- Back to Home »
- ASP.Net , C# , Generic Handler , Umbraco , umbracoSettings.config »
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Configuration;
- using umbraco.MacroEngines;
- using umbraco.cms.businesslogic.web;
- namespace KPMGAudit.Web.GenericHandlers
- {
- /// <summary>
- /// Summary description for UnpublishExpiredEvents
- /// </summary>
- public class UnpublishExpiredEvents : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- int EventsNodeId = Convert.ToInt32(ConfigurationManager.AppSettings["EventsNodeId"]);
- string strEventStartDate = string.Empty, strEventEndDate = string.Empty;
- context.Response.ContentType = "text/plain";
- dynamic EventsNode = new DynamicNode().NodeById(EventsNodeId);
- List<DynamicNode> eList = EventsNode.Children.Items;
- foreach (var Event in eList)
- {
- int CurrentEventNodeId = Convert.ToInt32(Event.Id);
- try
- {
- DateTime EventStartDate = Convert.ToDateTime(Event.GetProperty("eventStartDate").Value);
- strEventStartDate = EventStartDate.ToString("MM/dd/yyyy");
- }
- catch { strEventStartDate = string.Empty; }
- try
- {
- DateTime EventEndDate = Convert.ToDateTime(Event.GetProperty("eventEndDate").Value);
- strEventEndDate = EventEndDate.ToString("MM/dd/yyyy");
- }
- catch { strEventEndDate = string.Empty; }
- Document CurrentEventNode = new Document(CurrentEventNodeId);
- if (!string.IsNullOrEmpty(strEventEndDate))
- {
- if (Convert.ToDateTime(strEventEndDate) < DateTime.Now)
- {
- CurrentEventNode.UnPublish();
- }
- }
- else
- {
- if (Convert.ToDateTime(strEventStartDate) < DateTime.Now)
- {
- CurrentEventNode.UnPublish();
- }
- }
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
- public void UnpublishNode(Document CurrentContentNode, DateTime ContentExpiryDate)
- {
- if (ContentExpiryDate != DateTime.MinValue)
- {
- if (Convert.ToDateTime(ContentExpiryDate) < DateTime.Now)
- {
- CurrentContentNode.UnPublish();
- }
- }
- }
Configuration: (umbracoSettings.config)
- <scheduledTasks>
- <!-- add tasks that should be called with an interval (seconds) -->
- <!-- <task log="true" alias="test60" interval="60" url="http://localhost/umbraco/test.aspx"/>-->
- <task log="false" alias="ExpiredEventsUnpublishScheduler" interval="60" url="http://local.kpmg.com/GenericHandlers/UnpublishExpiredEvents.ashx"/>
- </scheduledTasks>