In a multi-lingual build we’d given the user a new toolbar button to create a copy of the given item in a given language. The logic behind the scenes would create a version of the new item in the destination language, scan the original item and copy all the field values to the destination. It would also then reset things like workflow on the destination item.
As part of this logic we’d call:
1 |
Item itemTo = item.Versions.AddVersion(); |
but would sometimes find itemTo would be null.
With the help of support, they pointed us towards the <sites> configuration and the following attribute:
filterItems: If true, the site will always show the current version of an item (without publishing) and advised this could be causing the issue. This setting can be manipulated programatically via setting:
1 |
Sitecore.Context.Site.DisableFiltering = true / false; |
So your final code would then be:
1 2 3 4 5 6 7 8 9 10 11 |
Item itemTo = null; bool oldValue = Sitecore.Context.Site.DisableFiltering; try { Sitecore.Context.Site.DisableFiltering = true; itemTo = item.Versions.AddVersion(); } finally { Sitecore.Context.Site.DisableFiltering = oldValue; } |
Enjoy your new versions!
Thanks for the post. This helps.