Set up your full-stack SitecoreAI local development environment

When you create a project using the Deploy app and choose the foundation template as the source for the initial deployment, the process adds the foundation template to the source control repository you configured for the initial environment.

You can use the resulting codebase to set up your local development environment based on Docker containers.

Note

Make sure you are using Docker v.2, as explained here.

To use the foundation template, you must have the following software components on your workstation:

This walkthrough describes how to:

Prepare the environment

Before starting the containers, you must ensure the necessary ports are accessible.

To prepare the environment for running the solution:

  1. Open PowerShell with administrator privileges.

  2. Check if you have the internet Information Server running on port 443:

    RequestResponse
    Get-Process -Id (Get-NetTCPConnection -LocalPort 443).OwningProcess

    If you do, you must stop it:

    RequestResponse
    iisreset /stop
  3. Check if you have Apache Solr or any other service running on port 8984:

    RequestResponse
    Get-Process -Id (Get-NetTCPConnection -LocalPort 8984).OwningProcess

    If you do, you must stop it:

    RequestResponse
    Stop-Service -Name "<the name of your service>"

    Alternatively, if you have started it with Non-Sucking Service Manager, stop it with the following command:

    RequestResponse
    nssm stop "<the name of your service>"

Configure and run the solution

The foundation template contains the software components and scripts to:

  • Update container images.

  • Build the containers.

  • Set up the necessary hosts.

  • Start the containers.

  • Start the Content Management (CM) instance.

  • Install the Sitecore CLI and the necessary plugins.

  • Populate the Solr Managed Schemas.

  • Rebuild indexes.

  • Serialize and push developer-managed items to the instance.

  • Open the CM instance and client app in the browser.

Important

Even if you plan on running multiple content management instances (multiple projects) locally, the hostname of every instance must follow the *.xmcloudcm.localhost pattern for authentication to work correctly. In other words, you can have hostnames such as myproject.xmcloudcm.localhost but not cm.mycompany.localhost.

XM Cloud is now SitecoreAI

Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.

To set up the template for running locally and start the containers:

  1. Open PowerShell with administrator privileges.

  2. In your project directory, clone the repository you configured for the project.

  3. Using a command-line interface, go to the root of your repository folder.

  4. To prepare the Sitecore container environment, run the script init.ps1 from the root directory of the project:

    RequestResponse
    ./local-containers/scripts/init.ps1 -InitEnv -LicenseXmlPath "<C:\path\to\license.xml>" -AdminPassword "<desired password>"
    Tip

    If you are not using the foundation template, you might not have the init.ps1 script in your project. You can copy the init.ps1 script from the starter kit repository.

  5. If you are using Windows 10, you need to align the container base images with what Windows 10 supports. To do this:

    • In the init.ps1 file, change the $baseOs version setting to ltsc2019.

    • In the .env file, replace the following variables with these values:

      RequestResponse
      TRAEFIK_IMAGE=traefik:v2.5.3-windowsservercore-1809
      
      NODEJS_PARENT_IMAGE=mcr.microsoft.com/windows/nanoserver:1809
  6. To download the Sitecore Docker images, install and configure the containers and client application, run the following script:

    RequestResponse
    ./local-containers/scripts/up.ps1
    Tip

    If you are not using the foundation template, you might not have the up.ps1 script in your project. You can copy the up.ps1 script from the starter kit repository.

  7. When prompted, in your browser, log in to the instance and accept the device authorization.

  8. If you are a member of multiple organizations, select the organization for the current project.

  9. Wait for the startup script to open browser tabs for the rendered site and the Sitecore Launchpad.

Important

If you have to reset the state of your containers, you can run the \docker\clean.ps1 script. It clears all mounted data and the deployed/copied build output. After running the .\docker\clean.ps1 script, you must rebuild search indexes.

Configure item serialization

If you inspect the content tree using the Content Editor tool while the local instance is running, you'll see that the local instance doesn't have any of the items for your new site. This is because the site was originally created in your remote environment, not in the local one, and the local instance doesn’t automatically include those items. You must therefore transfer the site items to your local and remote repositories so that every developer can access them.

You can synchronize items between environments with Sitecore Serialization. The repository already contains the necessary setup for enabling serialization if you are using an official SitecoreAI foundation template. It also includes the authoring/items/next-starter.module.json file that configures the rendering host item serialization. While developing locally, however, you need additional configuration.

To create a new serialization configuration:

  1. In the authoring/items/{SITE_NAME} folder, create a new serialization module file. Give it a meaningful name, for example, mysite.module.json.

  2. Replace the namespace value with a unique value, such as the site name. For example, MySite.

  3. Copy the content from the existing serialization module definition as a starting point, and modify the items property to include:

    • The placeholder settings:

      RequestResponse
      {
        "name": "layout",
        "path": "/sitecore/layout/Placeholder Settings/Project/placeholdersite",
        "allowedPushOperations": "CreateUpdateAndDelete"
      }
    • The templates created during the deployment process:

      RequestResponse
      {
        "name": "templates",
        "path": "/sitecore/templates/Project/MySite",
        "allowedPushOperations": "CreateUpdateAndDelete"
      }
    • The content item, which includes tenants and sites:

      RequestResponse
      {
        "name": "contentRoot",
        "path": "/sitecore/content",
        "allowedPushOperations": "CreateUpdateAndDelete"
      }
      Caution

      This configuration serializes the entire content tree because it does not have a single-item scope. When content authors start working on items, you must change this configuration to prevent overwriting their work when performing serialization push operations.

    • The media items:

      RequestResponse
      {
        "name": "media",
        "path": "/sitecore/Media Library/Project/MySite",
        "allowedPushOperations": "CreateUpdateAndDelete"
      }

    You can configure additional paths such as renderings, layouts, settings, and more, after they are created in the instance.

  4. Save the file.

Authorize within the SitecoreAI organization

Before performing operations against a remote SitecoreAI instance, you must authenticate within your organization.

To authorize the local project in your organization:

  • Run the following command:

    RequestResponse
    dotnet sitecore cloud login

    Follow the steps in the user interface to log in and authorize your device. When you finalize the authentication process, the connectivity information is stored in the .sitecore/user.json file.

Connect a SitecoreAI environment

You can connect to one or more remote environments.

To connect to an environment:

  1. Obtain the ID of your project by running the command:

    RequestResponse
    dotnet sitecore cloud project list

    Identify the project you are working on and copy the ID.

    Tip

    If you don't have a project, create a project in the Deploy app.

  2. Obtain the ID of your environment by running the command:

    RequestResponse
    dotnet sitecore cloud environment list --project-id <project-id>

    Identify the environment you want to connect to and copy the ID.

  3. Run the environment connect command:

    RequestResponse
    dotnet sitecore cloud environment connect -id <environment-id> --allow-write true

The process updates the .sitecore/user.json file with the environment connection configuration if the connection is successful. Assuming you named your environment dev, the .sitecore/user.json file now contains a dev key in the endpoints object:

RequestResponse
{
  "endpoints": {
    "xmCloud": {
      ...
    },
    "dev": {
        "ref": "XMCloud",
        "allowWrite": true,
        "host": "<the environment host>",
        "variables": {}
    }
  }
}
XM Cloud is now SitecoreAI

Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.

Connect the local environment

You connect the local environment with the Sitecore CLI connect command.

To connect:

  • In a terminal, go to the root directory of the local project and run the following command:

    RequestResponse
    dotnet sitecore connect --ref xmcloud --cm https://xmcloudcm.localhost --allow-write true -n local

The process updates the .sitecore/user.json file with the local environment connection configuration:

RequestResponse
{
  "endpoints": {
    "xmCloud": {
      ...
    },
    "dev": {
      ...
    },
    "local": {
        "ref": "xmcloudlocal",
        "allowWrite": true,
        "host": "https://xmcloudcm.localhost",
        "variables": {}
    }
  }
}
XM Cloud is now SitecoreAI

Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.

Pull serialized items from SitecoreAI to your local repository

With authentication/authorization in place and the necessary environment references added to .sitecore/user.json, your local project is ready to perform serialization operations.

To pull serialized items from the remote environment:

  • In the terminal, run the Sitecore serialization pull command, specifying the environment name as configured in the .sitecore/user.json file:

    RequestResponse
    dotnet sitecore ser pull -n "dev"

    The command output shows what items have been serialized.

Based on your serialization configuration, the source code now contains the src/items folder with the subfolders content-root, media, and templates.

Push serialized items into the local environment

Now that you have pulled the serialized items that were created in your remote environment from there into your local repository, you can push them into your local instance.

To push the serialized items into your locally running instance:

  • In the terminal, run the Sitecore serialization push command, specifying the local environment name as configured in the .sitecore/user.json file:

    RequestResponse
    dotnet sitecore ser push -n "local"

When the operation is complete, you can see the new items in your locally running instance. You can now commit and push the serialized items to your remote version control repository.

Note

The command serialization push (ser push) does not publish items. You must manually publish them.

Next steps

With this setup complete, you can start developing your SitecoreAI solution and your front-end application.

As you work, you can use the Sitecore CLI to perform common development tasks such as:

Do you have some feedback for us?

If you have suggestions for improving this article,