Skip to content
July 29, 2013 / kiranpatils

How to get valid version of an item using DateTime?

Challenge:

Before couple of weeks back, we wanted to modify a functionality where we wanted a valid version of an Item based on given DateTime (Basically, wanted to modify behavior of Version Selector Page which exists via Preview mode of Page Editor)

While doing that one more question also came in to our mind, During preview mode — when we select any date from Date/Version selector dialog, how Sitecore comes to know that it should give which version numbered item — by analyzing it a bit we found that it has some co-relation with sc_date cookie key. But by looking at code, we were unable to identify it!

So, we tried a lot, and finally decided to discuss our case with Sitecore support guys, and as always they replied us with a proper solution! Sharing with you!

Solution:

To get a valid version of an item use following method:

<item-object>.Publishing.GetValidVersion(<date-time>, false); // second argument, false deals with workflow — which we haven’t enabled

This method returns Item object which represents the suitable version of item.

Just a note: In your DateTime object, seconds also matters for example: If you have Version2 created on 12/07/2013 01:31:20 PM and If you pass — 12/07/2013 01:31:00 PM — It will not give you version2, instead it will give version1. Had a discussion with Sitecore support champs as well on this, and they told that in production environment it will not be the case that versions will be getting created in duration of a seconds. And I believe in a way they are right. But if it is not case for you. You can have a word with them!

How Sitecore resolved item’s version using sc_date? [Sitecore support reply]

In a nutshell, using the same API: by calling GetValidVersion method.
For more details, please refer to the following code in Sitecore (you can check it via Reflector):
Sitecore.Sites.SiteContext.ProcessQueryString()
Sitecore.Caching.ItemCache.GetItem(string key)

Basically, the selected DisplayDate influences the item data (which version) that is being retrieved.
I described an example with ItemCache, so when the item is requested, for example, in preview mode, the DateRange will be verified.
If there is no appropriate version in ItemCache, it will be retrieved from other layers e.g. from database via GetItemCommand class logic from Sitecore.Nexus.
As far as I know, this class has a similar check of date range to return valid version.

Easy stuff right? Just one line, That’s the power of Sitecore!

Happy Sitecoring! 🙂

PS : Special thanks to  Artem Fursenko(Sitecore Support) for providing this suggestion and Devashri who did all this investigation and worked on this challenge!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: