Copying content between databases in Sitecore

,

In this post I will describe some of the methods we have for copying items between databases in Sitecore. We can use these methods when content has been corrupted or is missing in one database but can be salvaged from another. If you are reading this post, you are probably in some extraordinary situation, so I hence try to keep everything short and to the point.

Let’s start by looking at the two ‘official’ ways of copying content between the same database on different Sitecore installations and between different databases within the same Sitecore installation:

Afterwards we will dive into a number of more advanced methods:

Except for the DB Browser, all of the options are ID-preserving, meaning that the ID of the copied items are kept, and existing content potentially removed based on the ID. The DB Browser method can be used ID-preserving and to create new IDs in the target database.

Sitecore packages

The normal way to move content from one Sitecore installation to another is via packages. Sitecore packages transfers data between the same database on two Sitecore installations in – e.g., from one master database to another master database.

You will find lots of guide for creating and installing Sitecore packages online. Sitecore packages are ID-preserving, and can be merged with existing content using a selection of merge methods.

The Package Designer in Sitecore allow you to pick and choose the items you want to include in your package. In salvaging scenarios, where you are creating packages containing lots of items, you might run into problems including huge number of static items recursively. If you face such issues, try adding it items dynamically instead:

Transfers items

Within a single installation the normal way to copy items from one database to another is via the transfer functionality. This functionality allows you to e.g., move an item from the web database to the master database:

Sitecore will present you with a dialog where we are able to select root in the target database and whether to include sub items. Please notice that dialog headline (Move an Item to Another Database) is misleading as the functionality will not remove the item from the source database – but copy it.

The transfer functionality is ID-preserving but does not offer the same merge options as Sitecore packages. This means that if an item with the same ID exists in the target database this item will be overwritten. However, if the item in the target database contains children not part of the same transfer operation, the operation will fail, as this potentially would leave these items without a parent (or delete them):

My experience is that the transfer functionality (which is by the way also available via the Control Panel) works well for small sets of items but can sometimes have problems with transferring large number of items.

Reverse publish

Another option for moving items between database – especially for moving items from the web database to the master database – is to do a reverse publish.

To publish from the web database to the master database, we need to temporary change the publishing target of the web database:

This will allow us to use the standard publishing functionality in Sitecore to transfer items, including subitems and related items.

The publishing functionality comes with all the normal restrictions (moving only latest publishable version). The operation is ID-preserving and will remove content from the target database not found in the source database (versions as well as sub items if we publish recursively), so this method needs to be used with care.

Retargeted Sitecore package

As described above, the database is ‘baked’ into a Sitecore package itself, so a package created from the web database will create items in the web database when installed on another Sitecore installation.

Sometimes however, we would like to generate a package from the web database and install it in the master database – either on the same or another Sitecore installation. To be able to do this we need to ‘retarget’ the package. A Sitecore package is a zip-like archive, containing another zip-like archive called package.zip. I am saying zip-like, because unzipping and then rezipping the archive will corrupt the archive and prevent Sitecore from accepting the package.

However, tools like 7-Zip allows us to edit the archive without extracting it, and while the package contains multiple references to the source database – both in files and in folder names – only two of them have to be changed to retarget the package to another database.

So, let’s say I have this structure in the web database and would like to move it to a master database via a retargeted Sitecore package:

I start by creating a package adding Home including subitems. I’ll then download the package and open it in 7-Zip:

I continue to open the inner package, and find this structure:

I now rename two folders within the package: items/web and properties/items/web – renaming both to master.

Finally, I click the back button until I am “outside” the inner package.zip. This will prompt 7-Zip to update the archive:

When this is done, I am now able to upload the retargeted package to Sitecore and install the items in the master database. Just as with normal Sitecore packages, the installation is ID-preserving, and allow me to merge content using the different merge options for packages.

DB Browser

Finally, the DB Browser (available via the Administration in the Control Panel) also allow us to copy and move items via XML, which can work for small number of items.

If we have this structure in the web database:

And wish to move the Item including the Subitem to the master database, we open the DB Browser, select the web database, and select the Item note in the left-hand content tree.

Under View xml, we select Show xml, and copy the XML produced to the clipboard.

We now change database and select the Home node in the master database. We paste the XML into the Paste xml section, deselecting Change IDs:

Clicking Paste xml will create the items from the XML into the master database, preserving the IDs. Any existing items under the same ID (e.g., other sub items of Item) will be replaced, so again this method needs to be used with care.

Oblivious, selecting Change IDs will generate new IDs for the master database, essentially creating a copy.

Summary

While the list above is not exhaustive, it offers a number of complimentary options for moving content around in Sitecore.

One option I have left out of the list is content serialization using Unicorn or Sitecore Content Serialization (SCS). These tools are normally meant to move content from one installation to another, but just as a Sitecore package can be retarget, so can the YML files used by both Unicorn and SCS.

However, with the methods described above we are able to perform a lot of rather advanced operations in regard to moving Sitecore content around.