Using the Sitecore CLI

In a previous post, I went over how to install the Sitecore CLI. In this post I’ll discuss using it for serialization.

Open powershell as administrator, change your directory to your solution directory, and enter “dotnet tool list”. If you see “sitecore”, you are ready to get started.

While still in your solution directory, run this command “dotnet tool run sitecore init”.

This will setup your solution for you. If you look at the directory structure, you’ll see a few things added.

  • [project directory]\.sitecore\schemasModuleFile.schema.json
  • [project directory]\.sitecore\RootConfigurationFile.schema.json
  • [project directory]\.sitecore\UserConfiguration.schema.json
  • [project directory]\.vscode\settings.json
  • [project directory]\.gitignore
  • [project directory]\sitecore.json

These items can be pushed into your repository. When a local developer pulls the repo, they will navigate to the solution directory and run “dotnet tool restore”. They can then run “dotnet sitecore” to confirm the CLI is ready and available.

sitecore.json

This is the solution configuration file.

{
  "$schema": "./.sitecore/schemas/RootConfigurationFile.schema.json",
  "modules": [
    "./src/*/code/.modules/*.module.json"
  ],
  "serialization": {
    "defaultMaxRelativeItemPathLength": 100,
    "defaultModuleRelativeSerializationPath": "../../serialization"
  }
}

Note that the serialization folder is relative to the modules. This creates a standard file system structure of src/feature/[feature name]/serialization which is next to src/feature/[feature name]/code assuming you put your feature or foundation project source in a folder called “code” under the respective feature or foundation folder.

Modules

Modules enable you to organize your serialized items by purpose or business domain. Modules will be organized according to your projects most likely. Much like a TDS project, you’ll most likely have a module for Content, Media, Core, and Master.

SampleSite.Project.Content.module.json
SampleSite.Project.Core.module.json
SampleSite.Project.Master.module.json
SampleSite.Project.Media.module.json

Let’s look at the contents of a module.

{
    "namespace": "SampleSite.Foundation.SampleProject",
  "references": [
    "SampleSite.Foundation.Common"
  ],
    "items": {
        "includes": [
          {
            "name": "Templates",
            "path": "/sitecore/templates/Foundation/SampleSite/Location"
          },
          {
            "name": "Tags",
            "path": "/sitecore/system/Settings/Rules/Definitions/Tags/SampleSite",
            "scope": "ItemAndDescendants"
          },
          {
            "name": "Elements",
            "path": "/sitecore/system/Settings/Rules/Definitions/Elements/SampleSite",
            "scope": "ItemAndDescendants"
          },
          {
            "name": "Conditional Renderings",
            "path": "/sitecore/system/Settings/Rules/Conditional Renderings",
            "scope": "ItemAndDescendants"
          }
        ]
    }
}

Notice the “references” array after namespace. These modules must be processed first before this module can be processed.

Syncing

Navigate to your solution directory in powershell (Adminstrator mode).

Assuming you’ve already installed or restored Sitecore CLI, log the CLI into Sitecore.

sitecore login --authority https://<identity authority> --cm http://<Sitecore instance> --allow-write true

Then using some basic serialization commands, you can begin to move items.

dotnet sitecore ser info
dotnet sitecore ser push
dotnet sitecore ser pull

That’s it. Happy Serializing!

About Phil Paris

Hi, my name is Phil Paris and I’m a Sitecore Architect and general Sitecore enthusiast. I’ve been working with Sitecore since 2013. Through this blog I will be sharing Sitecore insights, tips and tricks, best practices and general knowledge with the hopes to further the community at large. Please feel free to reach out to me at any time!

View all posts by Phil Paris →