Our organisation is working on a new content structure and took a decision to do this by creating a new content tree by cloning the present one. When it was time to go live with the new content we found that when deleting the clone ID we lost a lot of information on the clone item. The only way to save the information on the fields were to break the inheritance and then delete the dependency to the master item. Doing that manually would have taken a huge amout of time on thousands of items in 16 languages. This was a great opportunity to figure out how to use the Sitecore PowerShell Extension!
It is a good idea to first set up a dev environment with production data and run the script on that environment to prevent the risk of destroying prod data. When the script is finished running, the NewHome Clone node should be disconnected from the master. Then it is easy to create a ordinary Sitecore Package of the content node and import that to the prod environment using the replace alternative.
The Sitecore setup is like the following:
The renderings field {F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E} is set to not shared but enabled Language Fallback to be able for the local markets to have their own layout. (Sitecore 8 has better options to handle this with the Final Renderings field).
The global editors have created a new structure in the default EN language. The changes should reflect on the language versions.
The renderings on the language versions should be reset to the EN definitions but only if the EN renderings is not empty (has default values specified below)
This is what we would like to achieve:
Remove the clone connection
The content in Language version fields are reset to EN values in case they had the same value as the EN version.
The content in these fields are inherited from EN version using Language Fallback
The renderings field (the components on the item) is reset if it differ from the EN version but only if the EN version is not empty.
This is to make all the changes that are done on the EN version reflected on the language versions.
But some markets have their own localized items so they must remain
Display Name has been trimmed and all blank spaces at the trail are removed to make a proper URL without %20 or -
The script does the following:
Sets the start node to the NewHome item
Then iterate through all the children items and versions
Break the inheritance for the following fields on the cloned item (this is of course depending on the fields setup on your Sitecore environment)
Display Name
Title
Description
PromoHeader
PromoText
PromoImage
MenuTitle
Renderings (modules that is placed on the content page)
After that it deletes the clone connection
Now the NewHome is disconnected from the master tree!
Ok then, now we want to clean up the mess (if any).
Start another loop iterating through all the language versions of the NewHome node
Identify the EN version of respectively item
If the content in a language version field is the same as for the EN version, reset it. It is unnecessary to store the same info in the language version and it will also be more job later on not to be able to use Language Fallback; when editing an EN field we want it to automatically reflect on a language version field if the inheritance is not broken. We do this above on all the fields, but just in case there is an EN language version:
Display Name
Title
Description
PromoHeader
PromoText
PromoImage
MenuTitle
Renderings (modules that is placed on the content page)
Finally we reset the layout to reflect the EN version. But only in case of the following:
There is actually an EN version
The EN version is not empty (with no modules pasted on it)
This is because the markets have created language version items that is not present to the EN site. Sometimes we had to create an empty EN version to get the language version to be visible for some reason.
Additional to this, the script also trim the end of the display name in case there is any blank spaces added to the end of it. This is to get a proper URL with no %20 or – at the end.
Microsoft SQL Database Restore Script
I used this script to quickly restore the dev database with production data during the development of the PowerShell script.
[code language="sql"]
RESTORE DATABASE [DATABASE72Sitecore_Core]
FROM DISK = N'C:\Backup\DATABASE72Sitecore_Core.bak'
WITH FILE = 1,
MOVE N'Sitecore.Core.Data' TO N'C:\SITE\Database\MDF\DATABASE72Sitecore.Core.MDF',
MOVE N'Sitecore.Core.Log' TO N'C:\SITE\Database\LDF\DATABASE72Sitecore.Core.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO
USE [DATABASE72Sitecore_Core]
GO
CREATE USER [USER72] FOR LOGIN [USER72]
GO
USE [DATABASE72Sitecore_Core]
GO
EXEC sp_addrolemember N'db_owner', N'USER72'
GO
RESTORE DATABASE [DATABASE72Sitecore_Master]
FROM DISK = N'C:\Backup\DATABASE72Sitecore_Master.bak'
WITH FILE = 1,
MOVE N'Sitecore.Master.Data' TO N'C:\SITE\Database\MDF\DATABASE72Sitecore.Master.MDF',
MOVE N'Sitecore.Master.Log' TO N'C:\SITE\Database\LDF\DATABASE72Sitecore.Master.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO
USE [DATABASE72Sitecore_Master]
GO
CREATE USER [USER72] FOR LOGIN [USER72]
GO
USE [DATABASE72Sitecore_Master]
GO
EXEC sp_addrolemember N'db_owner', N'USER72'
GO
RESTORE DATABASE [DATABASE72Sitecore_Web]
FROM DISK = N'C:\Backup\DATABASE72Sitecore_Core.Web'
WITH FILE = 1,
MOVE N'Sitecore.Web.Data' TO N'C:\SITE\Database\MDF\DATABASE72Sitecore.Web.MDF',
MOVE N'Sitecore.Web.Log' TO N'C:\SITE\Database\LDF\DATABASE72Sitecore.Web.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO
USE [DATABASE72Sitecore_Web]
GO
CREATE USER [USER72] FOR LOGIN [USER72]
GO
USE [DATABASE72Sitecore_Web]
GO
EXEC sp_addrolemember N'db_owner', N'USER72'
GO
[/code]
The "cut the dependency of the Clone to the Master item"-script
The policy on our computer at work is that the Windows Login Screen shows up after a couple of minutes in case of user being inactive. Running the script and not working on the computer at the same time may cause the lock screen to show up and then the script may stop running in the background. I search for a solution on hacking the registry or local policy but the easiest way not conflicting with the companys policy (there is a reason for policies...) is to start the Windows Media Player, choose any music and run it using the repeat function. Then the lock screen will not appear and the PowerShell script may run util it is done! In my case it took about 5 hours...