Sitecore Lifetime fields and publishing in a nutshell - or an eggshell if you like
The Lifetime field section is not something that is used often but what I discovered can come in quite handy when combined with a scheduled publishing functionality. So, what are those lifetime fields and how can we use them? First of all, we need to make standard fields visible by checking Standard fields checkbox under View tab in Sitecore Content Editor. Then scroll down and right between Layout and Publishing is where this little gem is hidden.
To make use of these fields we need to have more than one version of the same item available. And I don’t mean language versions but rather ‘numbered’ chronological versions of an item. You can see in my screenshot that I have a warning around publishing that says that if I publish now the current version of my Image Component item will not be published because there’s a new kid in town – a more recent version of the same item. Ok fine, but what does it have to do with Lifetime?
If we set ‘Valid from’ and ‘Valid to’ fields to define a time period, we are telling Sitecore to only activate this version of our item for a specific period of time. What does that mean - ‘activate’? Is it going to disappear after ‘Valid to’ time comes? Well, technically it will! Let’s look at the following scenario.
Step 1. I made an Image Component data source item. And I chose an image of an egg in my Image field.
Version 1 is all I have to start with. I saved, I published – all is good. I see the egg when I view the published site. I see the same egg when I view my page in Preview mode as well. To differentiate things quicker let’s call refer to these two as CD and CM.
Step 2. I made a new version of the same item – Version 2. My content in this version is different – it now contains an image of a baby chick.
But this time I don’t want to publish it yet but leave it for later. What happens to my CM site? Do I see an egg or a chicken? I hit preview and I see a chicken. Makes sense, right? Because the latest chronological version is version 2 which is a chicken. Ok, but what about my CD site? Ann egg! Why? Because I have not published my chicken item yet. Also makes sense. So, what’s your point, Olga?
Let me do this: on my version 2 (chicken) I set ‘Valid from’ field to some time in the future. Say an hour from now. What do you think my CM site looks like now? Egg or chicken? I realize it’s a deeper philosophical question but still – what is it going to be? We are back to an egg on the CM. Because we essentially told Sitecore to keep version 2 of this item inactive for now until the time we indicated. How about CD? No changes there. Still see an egg.
Hopefully things are still making sense. But the question is – when are we going to see the picture of a chicken on CD? Don’t we have to publish? Let’s try it. I publish. I view the page. I see an egg. Still.
Why? Because Sitecore is not going to publish what’s not ‘active’. Our chicken is not ready to hatch out yet. 😊
I go get some coffee, I check my email, read some tweets and I come back and check both CM and CD. Now imagine the timestamp we used on our version 2 is now past. Guess what we see on CM? A chicken. Yes, finally! Sitecore activated version 2. What about CD? Same old egg.. .
Now let’s automate our publishing process to make things even more interesting. You can do this by either writing your custom logic, or you can use a module from the Marketplace, or you can activate Publishing Agent if it’s available to you. Publishing Agent can be set up to publish the whole site on a regular basis. Let’s set it to run every 15 mins as a test.
Let me go get more coffee. And read some more tweets. And yes, go through another handful of emails. Fifteen minutes go by. I check my CM again. No changes. Chicken has already been on CM. And look at that – I also see a chicken on CD. Nice! So, what just happened? What happened was because Publishing Agent ran and published our content it published the ‘active’ version of our item which now happens to be a chicken because chicken item got activated not too long ago, based on a timestamp we set in our version 2.
This functionality becomes quite useful in situations where content is intended to go live on a specific date. And combined with scheduled publishing it relieves our editorial team of their duty to remember to publish a new version of an item when time comes. Nice touch!