4
votes

In the site I'm working on, certain content only appears once a certain date is reached:

public bool IsActive
{
    get { return System.DateTime.Now >= this.IssueDate.DateTime; }
}

I want to test this code using the preview mode functionality and the experience bar. However, System.DateTime.Now always returns the real current date, regardless of preview mode. How do I write this so I can guarantee that the date set in the experience bar is respected, but while in normal mode the page still uses System.DateTime.Now?

Sitecore version: 6.4.1 rev. 110720

Update: I can't use publishing restrictions because children of this item need to be published and visible before this item appears in the menu that is controlled by this logic.

5
Anyone care to comment on why they are downvoting this question? What did I do wrong? Is it a bad question?Ed Schwehm
Most likely because you asked your question and immediately answered it, if you didn't need the help then pointless asking the question. It would have been better to ask the question and say "this is how I have implemented at the moment, is there a better way"jammykam
@jammykam: That seems an odd reason to downvote. Stackoverflow explicitly allows and encourages this behavior. Jeff Atwood even blogged specifically about this point: blog.stackoverflow.com/2011/07/…Ed Schwehm
Just my guess. I didn't vote either way. Can't see anything wrong with the question itself, it's useful info for others that may need it later.jammykam
@jammykam: Thanks for the feedback! That specifically was my intent; I had to Google for a while to get an answer, so I figured it was worth putting on Stackoverflow.Ed Schwehm

5 Answers

4
votes

Sitecore provides a pair of properties to aid in this situation:

    bool Sitecore.Configuration.State.Previewing
    DateTime Sitecore.Configuration.State.PreviewDate

The Sitecore.Configuration.State.Previewing property is true if preview mode is on, false otherwise. The Sitecore.Configuration.State.PreviewDate property returns the Sitecore.Sites.SiteContext.DisplayDate, which is set to the date in the experience bar in preview mode.

Here is the code I ended up using:

    public bool IsActive
    {
        get
        {
            DateTime dateToUse;
            if (Sitecore.Configuration.State.Previewing)
            {
                dateToUse = Sitecore.Configuration.State.PreviewDate;
            }
            else
            {
                dateToUse = DateTime.Now;
            }
            return dateToUse >= this.IssueDate.DateTime;
        }
    }

It appears to work fine without side effects.

1
votes

The best approach to handle something like this is instead to restrict content from being published until a certain date.

In the content editor, on the Publish tab, click the Restrictions button to restrict when content can be published.

If you need content to go live at a certain date and time, you can leverage the Automated Publisher module for this.

1
votes

You should be able to get the selected date by using WebEditUtil.GetCurrentDate().

If you don't wan't to reference that, check the cookie "sc_date".

1
votes

I don't think using Sitecore.Configuration.State.PreviewDate alone will cause any issues as it always returns the dateTime ( of when the request is made).

EDIT: In case of AJAx calls to the same page/Code Sitecore.Configuration.State will be null, so make sure you handle it.

0
votes

An alternative is to update the SystemTime.CurrentTime method. In Global.asax.cs, in Application_BeginRequest, add the following:

SystemTime.CurrentTime = () =>
                State.Previewing || State.WebEditing
                    ? State.PreviewDate
                    : DateTime.Now;

Now anywhere you seek DateTime.Now it will return the proper date.