Content scheduling in web parts
This article gives you instructions on how you can implement content scheduling in web parts similar to scheduling you are familiar with from documents. In other words, you will see how you can specify when the content rendered by the web part will be published for version 5.x, and 6.x of Kentico CMS. Please see this link -
devnet.kentico.com/articles/content-scheduling-in-web-parts-v7 - for version 7.x specific instructions.
You need to start with adding two properties to a web part you want to enable scheduling for. You can do so in
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
In order to avoid modifying the code of web parts, we will put the logic of the scheduling to a custom macro which will be used in the Visible property of the web part. In the code of the custom macro you can access values of the publishfrom and publishto fields. Then you can compare them to the DateTime.Now value and return true or false accordingly.
So in general, the macro could look like the following one:
switch (expression.ToLower())
{
case "visibilitymacro":
match = true;
result = "false";
if (sender.SourceTables[0].ContainsKey("publishfrom") && sender.SourceTables[0].ContainsKey("publishto"))
{
DateTime publishFrom = ValidationHelper.GetDateTime(sender.SourceTables[0]["publishfrom"], DateTime.MinValue);
DateTime publishTo = ValidationHelper.GetDateTime(sender.SourceTables[0]["publishto"], DateTime.MaxValue);
if ((DateTime.Compare(publishFrom, DateTime.Now) < 0) && (DateTime.Compare(publishTo, DateTime.Now) > 0))
{
result = "true";
}
else
{
result = "false";
}
}
break;
}
Update for version 6:
switch (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;
}
You can find the
~\App_Code\Global\CMS\CMSCustom.cs file with the custom macro attached as well as an
export of the CustomRepeater web part with properties
publishto and
publishfrom.
-ml-
See also: Developing web parts,
Custom macro expression
Applies to: Kentico CMS 5.5R2, an update for version 6