Initial field values for Sitecore – Setting a default future date

You all know that you can insert the node name in a text field by writing $name in the __Standard Values:

Node name as initial value

Node name as initial value

This will fill in the node name when the item is created. Sitecore has more of those built in:

  • $name = Node name
  • $id = Item id
  • $parentid = Item parent id
  • $parentname = Parent node name
  • $date = Current date
  • $time = Current time
  • $node = Current date and time

I have a scenario where I would like to create my own initial value. I would like to set a default future date in a field, like this:

Date in future as a initial value

Date in future as a initial value

My “futureDate” initial value grabs the years, months and days and add those to the current day to set a date in the future. In the example above I set the date to Now+1 year.

To do this I modify the expandInitialFieldValue pipeline (the first 2 processors are from Sitecore, the last one contains my code):

<expandInitialFieldValue help="Processors should derive from Sitecore.Pipelines.ExpandInitialFieldValue.ExpandInitialFieldValueProcessor">
  <processor type="Sitecore.Pipelines.ExpandInitialFieldValue.SkipStandardValueItems, Sitecore.Kernel" />
  <processor type="Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel" />
  <processor type="MyCode.Pipelines.ExpandInitialFieldValue.ReplaceDateInFuture, MyDll" />

Your own replacer token must start with a $ and cannot look like any existing tokens, as the Sitecore code uses pattern matching to get the correct replacer value. If I called my token $dateFuture, Sitecore would hit it, replacing $date with the current date, leaving “Future”. that’s why I call my token $futureDate.

The code is simple pattern matching and string replacing (you should apply your own exception handling to the code):

using System;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.ExpandInitialFieldValue;

namespace MyCode.Pipelines.ExpandInitialFieldValue
  /// <summary>
  /// Pipeline processor for the ExpandInitialFieldValue pipeline that allows me to
  /// set a date in the future for an item.
  /// </summary>
  public class ReplaceDateInFuture : ExpandInitialFieldValueProcessor
    /// <summary>
    /// Processes the specified args.
    /// </summary>
    /// <remarks>
    /// The format of the replacement string is the following:
    /// $futureDate(years,months,days)
    /// where:
    /// - years = years to add
    /// - months = months to add
    /// - days = days to add
    /// For example:
    /// $futureDate(1,0,0)   = Adds DateTime.Now + 1 year
    /// $futureDate(0,6,0)   = Adds DateTime.Now + 6 months
    /// $futureDate(1,6,12)  = Adds DateTime.Now + 1 year, 6 months, 12 days
    /// </remarks>
    /// <param name="args">The args.</param>
    public override void Process(ExpandInitialFieldValueArgs args)
      Assert.ArgumentNotNull(args, "args");
      if (args.Result.ToLower().StartsWith("$futuredate"))
        string[] yymmdd = args.Result.ToLower().Replace("$futuredate(", "").Replace(")", "").Split(',');
        if (yymmdd.Length != 3)
          throw new Exception("Invalid $futureDate format. Expected: $futureDate(years,months,days) (for example $futureDate(1,0,0)). Found: " + args.Result);
        args.Result = DateUtil.ToIsoDate(DateTime.Now.AddYears(GetInt(yymmdd[0], 0)).AddMonths(GetInt(yymmdd[1], 0)).AddDays(GetInt(yymmdd[2], 0)));

    /// <summary>
    /// Safe string to int conversion
    /// </summary>
    private int GetInt(string s, int defaultValue)
      int val;
      return int.TryParse(s, out val) ? val : defaultValue;


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, Sitecore 6 and tagged , , , , . Bookmark the permalink.

4 Responses to Initial field values for Sitecore – Setting a default future date

  1. Lieven Praet says:

    Nice and simple :)
    Good work man!

  2. John West says:

    I think $node should be $now.

  3. Pingback: How do I get the Publishing date for a page in Sitecore CMS? | Eldblom

  4. Leo says:

    Agreed: $node should be $now

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s