Fixing Standard Values without Affecting Existing Content

l-31360-if-rum-cant-fix-it-youre-not-using-enough-rumUnfortunately I wasn’t able to attend Sitecore Symposium this week so won’t be writing up an overview of what I’ve learned, there were some exciting things announced for Sitecore in 2020 though from what I’ve seen on Twitter so I’ll keep an eye out for those Symposium Blog posts for more info on what I missed.

Instead this Blog Post is about an slightly odd situation regarding fixing Standard Values when they have been set incorrectly for a number of templates.

Standard values provide initial values for all items based on a given data template.

We had around 30 page data templates that have an field value set on the standard values to an incorrect value. Unfortunately there are thousands of items based on these templates and I needed to maintain the existing values so I couldn’t just change the standard values to the right ones as it would also re-set all items still using the Standard Values.

Reading up on How Standard Values Work again the import part is this:

A field can contain the same value as its standard value without actually containing that standard value. For example, an item contains its standard value. The user updates that field; the field no longer contains its standard value.

The user updates the field again, setting the value to the same value as the standard value for the field, but without resetting the field to its standard value. The field now contains the same value as its standard value; however, it does not contain its standard value.

So I decided that SPE would be the perfect tool to solve this issue. I initially wrote an script to update the item field value to another value and then a 2nd script to set it back to the same value as the standard value. This means the item would no longer be inheriting the field value from the Standard Values.

However this wasn’t very efficient and there was a risk of my items loosing the correct value when being set back again. My colleague Mobeen suggested that perhaps if I updated the script to set the value to something and then immediately back to the same value as the standard value then this would work and only require one script. It did and here is the final script I came up with:

The script makes use of the .ContainsStandardValue property of a field which Sitecore uses to track if a field is using the Standard Value or not. Once the script has run the ContainsStandardValue for the field is false for all items and therefore the Page template can be updated without affecting them.

You will see it sets a field called ‘content type’ so to re-use it you should change the field name used. Also ensure you set the path to the items in the pages query.
You can run this with the value of $global:updateContentTypeField set to false to just report which items are using the standard values for this field. If you wish to update the field value for items then set this to true.

Once this had run I was safe to correct the standard values on all my page templates without affecting the existing content :-).

Hopefully this will help someone else who has this issue.