Teaching Sitecore: Ensure that items have at least one version

For 2015 I will be focussing a lot of blog posts on Teaching Sitecore. Since there is no freely available version of Sitecore to help developers learn the platform, many new devs are thrown in to the Sitecore world with little or no experience. This is a real problem for Sitecore partners and customers and illustrates the importance of Sitecore mentorship as a key to success.

I’m sure many Sitecore developers learned this lesson the hard way just like me. Always check to make sure a Sitecore item has at least one version in the context language before using that item in your code. The reason for this is that Sitecore would gladly publish items to the web DB whether the item had 100 versions or 0 versions. This is something that I made sure all new Sitecore devs were aware of because it’s not exactly intuitive functionality and it’s something that may not be fully apparent until you start using multiple languages or workflows.

With the release of Sitecore 7.2 and all its new publishing features, there was a setting that changed this functionality. The “Publishing.PublishEmptyItems” setting is set to false by default and will ensure that items must have at least one publishable version before being eligible for publish.

     Specifies whether empty items (i.e. items without publishable versions) should be published.
     Default value: false
<setting name="Publishing.PublishEmptyItems" value="false" />

Taken from the Sitecore 7.2 Dev Team – The setting also existed in previous versions, but you had to manually add it to the configuration file. In previous versions, the default value was true, causing items that did not have any publishable versions to be published along with all of their shared fields. With 7.2, the default value is false, so that empty items are not published.

So how does this change things for developers in Sitecore 7.2 and above?

The important thing to remember is that the new “Publishing.PublishEmptyItems” setting is helpful if you are building a uni-lingual site with one language but does not resolve the issue if you are using multiple languages. Items will still be published as long as they have one publishable version in ANY language. For example, you may have an item that has a version in English but has 0 versions in French or Spanish.

Is this an improvement?

I would argue that this is actually worse for developers because they may not fully understand how this works while they develop the site in a single language. It is quite common to do the majority of development in one language and then focus on the second language in a later development phase. I may go as far as to set the “Publishing.PublishEmptyItems” setting to true for future projects so that this issue is more visible to new developers earlier on in development.

What is best practice?

It is important to teach all new developers about how Sitecore handles publishing of items and versions so they understand why following best practice is so important.

Even in newer versions of Sitecore it’s not enough to do null checks on items in many situations.

if (item != null)

You also need to ensure that the item has at least one version in the context language.

if (item != null && item.Versions.Count > 0)

Posted in Sitecore

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 )

Connecting to %s

About Me
My name is Scott Mulligan and I am currently working as a software developer for Sitecore out of Ottawa, Canada.
Sitecore Technology MVP