Sitecore Development / Kim Hornung

Friday, March 23, 2007

A note on multiple save events per save operation

Recently I was fiddling around with the "item:save" event in Sitecore. And I noticed that in some situations, multiple save events are raised for a single save operation.

At that time, I simply figured that Sitecore’s UI might update different parts of the item in different operations (that does not make too much sense, but I was satisfied and yet again able to sleep at night).

But this week, I learned that this is implemented "by design".

What really happens is that Sitecore raises a save event for each version in all languages if you make any changes to a "shared" field (a field that only has one value for all versions in all languages). And if you make changes to a "versioned" field (a field that has one value per language), a save event will be raised for each version in the current language.

Monday, March 05, 2007

Fun with Standard Values and versioning

Sitecore v5.3 introduced the concept of "Standard Values" which gives you dynamic, default values for the different data fields for each template in Sitecore. A really excellent feature that can be used for things like assigning masters to template, specifying default categories for news items, etc. etc.

At a recent SCD1 training, a developer asked me whether it is possible to create several versions for the standard values. In other words: Can you make the "fall-back" value change over time? Or just keep track of your previous standard values?

I was a bit sceptic, but Sitecore stores the standard values as any other item in Sitecore, so it might work.

So what happened?
I created a simple test case:

  1. First I added some random text to version 1 of the Standard Values for the "Page" template of my trusty demo site

  2. I tested that this worked as expected when showing a Preview (it did, which is hardly surprising since this is the normal way of using Standard Values)

  3. Then I added a second version to the Standard Values and filled in some text

  4. I opened a Preview and was happy to see that the version 2 values was displayed

  5. Now the fun part: I entered a start date for version 2 in the "Publish Restrictions" dialog box so that version 2 was set to be published in a week from now. That gave a quite unexpected result!

After specifying the publishing date, the menu on my website suddenly looked like this:

Notice how "KH TEST" and "Company History" are still displaying their normal menu title, while all other menu items are displaying the data from the Standard Values.

But why does the Standard Values override the values for all these items???

It took me a few seconds to figure out what is happening:

  1. When Sitecore resolves the fields for an item, it look for a Standard Value for all data fields that are NULL
  2. Most of the items on the demo site has a NULL value for all the "Lifetime" fields (i.e. the fields that specify the publishing dates for each individual version)
  3. Therefore, Sitecore will use the Standard Value for the "Liftetime" for all these items - and since the Standard Value has a publishing date in the future, Sitecore will not display the regular data in the fields (but will instead display any Standard Values for the data fields, disregarding the fact that publishing dates have been specified for the Standard Values item)

So what happens is very logical (maybe except for the fact that the standard values apparently doesn't respect publishing restrictions). But it was definitely not the result I expected :o)

Conclusion:
  • It is possible to create multiple versions for Standard Values in Sitecore (for example to keep a backup of the previous values), as long as you don't specify any publishing restrictions (such as specifying publishing dates for the most recent version of the Standard Values item).
  • Specifying publishing dates for Standard values will sooner or later lead to confusion!
    All items with a "NULL" value in the publishing dates will use this date, and their regular content will only be available within the publishing dates that are specified for the Standard Values (outside this interval, older versions of the item will be displayed if they have explicit publishing dates specified - if no explicit publishing dates are specified for any versions, the Standard Values will be displayed instead).