Content scheduling in web parts V7

   —   
This article expands upon the original Content scheduling in web parts by Helena, with updated code for version 7.x of Kentico CMS.  The code below is only intended to work with version 7.x of Kentico CMS, if you are looking for version 5.x, or 6.x please see the original article - devnet.kentico.com/articles/content-scheduling-in-web-parts
You can create a new class in your App_code folder for this custom macro and use the code below for version 7.x:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using CMS.SettingsProvider; using CMS.GlobalHelper; using CMS.CMSHelper; /// <summary> /// Summary description for CustomMacroLoader /// </summary> [CustomMacroLoader] public partial class CMSModuleLoader { /// <summary> /// Attribute class ensuring the registration of macro handlers. /// </summary> private class CustomMacroLoader : CMSLoaderAttribute { public override void Init() { // Assigns a custom macro resolving handler. MacroResolver.OnResolveCustomMacro += MacroResolver_OnResolveCustomMacro; } /// <summary> /// Resolves custom macros. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event arguments representing the resolved macro</param> private void MacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e) { // Checks that the macro is not resolved yet. if (!e.Match) { // Defines the return values of specific custom macro expressions. switch (e.Expression.ToLower()) { case "visibilitymacro": e.Match = true; e.Result = "false"; ContextResolver macro = (ContextResolver)sender; if (macro.SourceTables[0].ContainsKey("publishfrom") && macro.SourceTables[0].ContainsKey("publishto")) { DateTime publishFrom = ValidationHelper.GetDateTime(macro.SourceTables[0]["publishfrom"], DateTime.MinValue); DateTime publishTo = ValidationHelper.GetDateTime(macro.SourceTables[0]["publishto"], DateTime.MaxValue); if ((DateTime.Compare(publishFrom, DateTime.Now) < 0) && (DateTime.Compare(publishTo, DateTime.Now) > 0)) { e.Result = "true"; } else { e.Result = "false"; } } break; } } } } }

Once you have added the code, you can add two new fields to your web part via Site manager -> Development -> Web parts -> edit web part -> Properties:

Attribute name: publishfrom
Attribute type: Date and time
Allow empty value: true
Display attribute in the editing form: true
Field caption: Publish From
Field type: choose according to your needs



Attribute name: publishto
Attribute type: Date and time
Allow empty value: true
Display attribute in the editing form: true
Field caption: Publish To
Field type: choose according to your needs


If you would like to add these fields to every web part within your CMS, you can edit the ~\App_Data\CMSModules\PortalEngine\Properties\_Groups\Visibility.xml file and add these fields directly to the XML file.

In our first example we will add the visibilitymacro to the Visible condition and leave the Publish dates blank.


The text is still loaded to the page as it should be, since we haven’t specified any Publish dates.


Now let’s set the Publish To date to a date in the past, to confirm our content will be hidden.


As you can see our content is no longer visible, since we set the Publish To date in the past.
Share this article on   LinkedIn