Store Item "Published date" and "Published by" information on each Sitecore item
In this post I'll explain how you can store the "Published date" and "Published by [user]" information on Sitecore items once such have been published by a user, in order to be viewable by any content manager or Sitecore admin in the backend.
The solution is based on an article by Mike Reynolds in 2013 – however, the code provided by Mike doesn't work correctly in a multi-version and multi-language instance. So I fixed that.
Let's dig in: extend your Item Statistics Section in Sitecore
I don't want to extend the original item template because any version update of Sitecore will destroy those customized changes. So I made a new custom item section and let the original template inherit from it:
Now I know what you might think:
…but a version update will revert THOSE changes too!?
That's basically true, but I synched the original Statistics section with Unicorn – however, EXCLUDING it's children. So any changes should not affect my customized changes :)
Configure a custom Statistics section
The original Section template can be found under this path:
- /sitecore/templates/System/Templates/Sections/Statistics
Let's add a new, custom one under the following path:
- /sitecore/templates/User Defined/CustomerName/Sections/Statistics Section
- Important: use the correct name for the section – "Statistics"! Otherwise it will not be add correctly to the existing Sitecore statistics section.
Please note: it's very, very, very important, that this section doesn't inherit from the standard template! Because this would mess up the whole item structure database-wise - causing your Sitecore solution not to work anymore. Believe me, I've been there ;)
So, instead, use {00000000-0000-0000-0000-000000000000} as base template.
Required field configurations for the new section
We go ahead and add the following two fields to the template. Name the fields with two underscores:
- __Published
- __Published by
And once done & saved, change the title of each field back to "Published" and "Published by" (without underscores)
- Published
- Published by
Now it's time to inherit...
Let the original Sitecore statistics section inherit from the new one:
- /sitecore/templates/System/Templates/Sections/Statistics
As you can see, that template is under Unicorn sync.
And again: before you add the new section to the Sitecore system section as base template make sure the new custom section doesn't inherit from the "standard template" – or you'll say "Hasta la vista" to your Sitecore database!
Keeping everything in-sync
Don't forget to add the original Sitecore system section to the Unicorn sync – excluding it's children.
Unfortunately, there is no transparent sync possible if there are excludes for the children (at least to-date). So you have to run a Unicorn sync manually if you deploy this to another solution.
If you use unicorn sync in general, it's mandatory to add a fieldFilter for the two new fields. Otherwise any publish on an item will lead to a changed yml-file of this or any related items, which then will be committed to your git/versioning tool. That's why we add the two fields as fieldFilter to the Unicorn config. Now, every item in Sitecore should have this two new fields in the "Statistics" section.
Time to create a custom PublishItemProcessor
Configure a custom PublishItemProcessor-pipeline:
- Sitecore.Publishing.Pipelines.PublishItem.PublishItemProcessor
The original code from Mike, in our test solution, resulted in a problem that a new language version "de-DE" would be created with the "Published" and "Published by" values. And we do not want this to happen – unless such a item version already existed before the publish ;) Or it could be that this behaviour is a problem of our Sitecore 8.0 Initial version. Anyhow, here's a fixed code snippet overcoming this side effect:
Compared to Mike's script, I enhanced it in order to allow it to get the right language-version-combination which will be updated on a publish.
The code is executing the following list of priorities:
- Take item with same language and same version
- if not found, take item with same language
- if not found, take item with language "en" as fallback (because all our templates etc. are created in English)
Patching the original PublishItemProcessor
Go ahead an add the PublishItemProcessor after the UpdateStatistics PublishItemProcessor in a new patch config file:
The PublishItemProcessor sets the publishing statistics on the newly added fields on the item in both, the source and target databases, and only if the publishing fields exist on the published item!
That's it – we now have additional publish details when publishing items!
Now, every item publish results in saving the Sitecore username to the "Published by" and the current timestamp to the "Published" fields in the statistics section.