Build
Sitecore Content Serialization

Sitecore Content Serialization (SCS)

At this point, it is a good idea to learn about content serialization. As a quick refresher, content items lives in Sitecore databases: the master database for work in progress content, and the web database for published content. Content serialization is the process of exporting content items and their associated media, templates, layouts, and other site-related information from a Sitecore instance to a serialized format that can be easily transported and imported into another Sitecore instance.

sitecore content serialization

Content serialization is often used in development and deployment workflows to move content between different environments, such as from a local development environment to a testing or production environment. It is also useful as a means to sync content between multiple developers. When working on a CMS site such Sitecore, you not only need the code to be in sync, but also the content. Content serialization provides a means for that and you would check in the files just like you check in code to the source repository. The serialization format is typically YML, and there are several third party serialization tools available in Sitecore such as Unicorn and TDS.

Starting with Sitecore 10.0 initial, Sitecore introduced their own version called Sitecore Content Serialization (opens in a new tab).

Authorize access

The first step to serializing content is to authorize access using the login command.

dotnet sitecore login --authority https://<sitecore-identity-server> --cm https://<sitecore-instance> --allow-write true

If you have been following our tutorial,

  1. On C:\repos\XmNextJs, run the command:
    dotnet sitecore login --authority https://id.xmnextjs.localhost --cm https://cm.xmnextjs.localhost --allow-write true
  2. The browser will open a Sitecore window asking you to login to Sitecore. Log in.
  3. Click "Yes, Allow" allow cm login

Serialize content from the Sitecore database

We have created quite a few content items such as templates, renderings, and others. Let us serialize content from the database. sitecore content serialization - dotnet sitecore ser pull We do this with the command:

  1. run dotnet sitecore ser pull dotnet sitecore ser pull

  2. Noticed that SCS created yml files which represents each item in the Sitecore database onto the src\items directory. This is what should be checked in.

Push content from serialized files into the Sitecore database

To do the reverse and push content from serialized files into the Sitecore database, we use the command:
dotnet sitecore ser push sitecore content serialization - dotnet sitecore ser push

Development workflow

In a typical development work flow, you would

  1. Get the latest code from a branch (typically develop)
  2. Create a feature branch from the branch
  3. Run dotnet sitecore ser push to push changes other developers made to your Sitecore database
  4. This ensures that both code and content is in sync with other developers
  5. develop your feature
  6. Run dotnet sitecore ser pull to serialize your content
  7. Commit your branch and create a pull request

Sitecore Content Serialization configuration

How does Sitecore know which content items to serialize? Sitecore utilizes json files to control what content to serialize. In C:\repos\XmNextJs\src, notice there are 3 .modules.json files.

We'll take a look at the InitItems.module.json as an example.

InitItems.module.json
{
    "namespace": "InitItems",
    "items" : {
        "includes": [
            {
                "name": "content-root",
                "path": "/sitecore/content",
                "scope": "singleItem",
                "allowedPushOperations": "createAndUpdate"
            },
            {
                "name": "api-key",
                "path": "/sitecore/system/Settings/Services/API Keys/XmNextJs",
                "scope": "singleItem"
            },
            {
                "name": "language",
                "path": "/sitecore/system/Languages/da-DK",
                "scope": "singleItem"
            }
        ]
    }
}
  1. namespace - organize the configuration using namespace
  2. items - items this configuration applies to
    1. includes - what items to include into the serialization
      1. name - a descriptive name for the item
      2. path - the sitecore path to the item in sitecore
      3. scope - SingleItem, ItemAndChildren, ItemAndDescendants, or DescendantsOnly
      4. allowedPushOperations - CreateOnly, CreateAndUpdate, or CreateUpdateAndDelete

Learn more about Sitecore Content Serialization configuration (opens in a new tab).

💡

Knowledge check:

  1. What is content serialization?
  2. What are some uses of content serialization?
  3. How do we serialize content?
  4. How do we push content to the Sitecore database?