Skip to main content

Sitecore

Sitecore Content Serialization Series – Episode 2 – Setup using CLI (Command Line Interface)

Fotis Fotopoulos Duhkov44prg Unsplash

CliIn the previous blog, we talked about Introduction to Sitecore Content Serialization, and in this blog, we are going to discuss the configuration required for Sitecore CLI. The Sitecore command line interface is newly introduced with Sitecore 10 release. This tool helps us to communicate with the Sitecore instance and push/pull and publish the serialized content through the command line interface. Using this tool, the serialization of the content is fast, it works extremely well remotely, and it supports optimized developer experience. CLI works on commands, and it doesn’t require any license, whereas Sitecore for Visual Studio requires TDS License. But other than that, there are no such differences in the tools; both tools provide the same functionality and ability to perform all the required features needed for serialization. Sitecore CLI can install either .NET local or global tool, which enables all the features of serialization.

Note: If it’s your first time using CLI on your machine, then you need to install Sitecore Management Service.

 

Steps to Install/Configure Sitecore CLI

Step 1: Open the PowerShell terminal with administrative privilege.

Step 2: Open your project folder in the terminal and hit run the below command.

    • The command for local setup
dotnet new tool-manifest
dotnet tool install Sitecore.Cli --version 2.0.0 --add-source https://nuget.sitecore.com/resources/v3/index.json
    • The command for global setup
dotnet new tool-manifest
dotnet tool install Sitecore.Cli -g --version 2.0.0 --add-source https://nuget.sitecore.com/resources/v3/index.json

Step 3: Establish the handshake and login to the Sitecore instance.

    • You need to collect the below parameters to log in with the Sitecore instance
      • Sitecore instance URL (example https://myproject.local)
      • Sitecore identity service URL
      • Sitecore username
      • Sitecore password
    • Also, there are two ways to validate the user request,
      • Interactive user login
        • Run the below command to validate the user
          dotnet sitecore login --auth <Sitecore-Identity-URL>--cm <Sitecore-Instance-URL> --allow-write true
        • dotnet sitecore login command opens the Sitecore login page(open the Sitecore identity url mentioned in the above command); use your Sitecore username and password to log in.
      • Non-interactive user login
        • Create a new file in the Sitecore identity server config filer, named Sitecore.IdentityServer.DevEx.xml. And add the below line of code into it and update the parameters as per your configuration. The Client is configured in Sitecore Identity Server and the identity provider is configured in the content management.
          <?xml version="1.0" encoding="utf-8"?>
          <Settings>
            <Sitecore>
              <IdentityServer>
                <Clients>
                  <!-- Authenticate servers with ClientID and ClientSecrets -->
                  <CliServerClient>
                      <ClientId><SitecoreCLIServer-Enter unique id of your client></ClientId>
                      <ClientName>SitecoreCLIServer</ClientName>
                      <AccessTokenType>0</AccessTokenType>
                      <AccessTokenLifetimeInSeconds>3600</AccessTokenLifetimeInSeconds>
                      <IdentityTokenLifetimeInSeconds>3600</IdentityTokenLifetimeInSeconds>
                      <RequireClientSecret>true</RequireClientSecret>
                      <AllowOfflineAccess>false</AllowOfflineAccess>
                      <AllowedGrantTypes>
                          <AllowedGrantType1>client_credentials</AllowedGrantType1>
                      </AllowedGrantTypes>
                      <ClientSecrets>
                          <!--The maximum supported length for a client secret is 100 characters -->
                          <!--<ClientSecret1>SUPERLONGSECRETHERE</ClientSecret1>-->
                      </ClientSecrets>
                      <AllowedScopes>
                          <!-- Required -->
                          <AllowedScope1>sitecore.profile.api</AllowedScope1>
                      </AllowedScopes>
                  </CliServerClient>
                </Clients>
              </IdentityServer>
            </Sitecore>
          </Settings>
        • Create a new file in the Sitecore instance App_Config/Include folder, named Sitecore.Owin.Authentication.ClientCredentialsMapping.config. Add the blow line of code into it and update the parameter as per your instance configuration.
          <?xml version="1.0" encoding="utf-8"?>
          <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
            <sitecore role:require="Standalone or ContentDelivery or ContentManagement">
              <federatedAuthentication>
                <identityProviders>
                  <identityProvider id="SitecoreIdentityServer" type="Sitecore.Owin.Authentication.IdentityServer.IdentityServerProvider, Sitecore.Owin.Authentication.IdentityServer" resolve="true">
                    <transformations hint="list:AddTransformation">
                      <transformation name="admin-ify client credentials users" type="Sitecore.Owin.Authentication.Services.DefaultTransformation, Sitecore.Owin.Authentication">
                        <sources hint="raw:AddSource">
                  <!-- Update the client_id in the below line with the unique id of your client -->
                          <claim name="client_id" value="SitecoreCLIServer" />
                        </sources>
                        <targets hint="raw:AddTarget">
                          <claim name="name" value="sitecore\superuser" />
                          <claim name="http://www.sitecore.net/identity/claims/isAdmin" value="true" />
                        </targets>
                        <keepSource>true</keepSource>
                      </transformation>
                    </transformations>
                  </identityProvider>
                </identityProviders>
              </federatedAuthentication>
            </sitecore>
          </configuration>
        • Now you need to restart both identity and CMS sites and recycle the app pool as well.
        • Just to validate the above configuration, run the below command.
          dotnet sitecore login --authority <Sitecore-Identity-URL> --cm <Sitecore-Instance-URL> --allow-write true --client-credentials true --client-id <client-id> --client-secret <client-secret>

Review the below table to see all the available commands for Sitecore CLI (Note – prefix to all below commands dotnet sitecore)

  • login – Validate the user before establishing a connection with the Sitecore instance.
  • init – Initialize a project configuration in the current folder
  • ser pull – Serialized content items from your Sitecore instance to your project file
  • ser push – Serialized content items from your project file to the Sitecore instance
  • publish – Publish all content items to the web database
  • ser diff – Compare the content item between two Sitecore instance
  • ser info – Show configuration information
  • ser package – Lists of serialization package commands
  • ser package create – Create a package of the serialized content item
  • ser package install – Install a package of the serialized content item
  • ser validate – Validate the serialized content item, and the suffix –fix will resolve any errors.
  • ser explain –  Explains whether a content item path is included and why.
  • ser watch – Monitors the changes to a content item in the instance and serializes the changes to your project file.

Please check out my next blog for Setting up the Sitecore Content Serialization using Sitecore for Visual Studio and stay tuned for more blogs regarding Sitecore Content Serialization and feel free to add a comment below if you have any questions or comments.

To Be Continued!

Happy Learning 🙂

#sitecore

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sumit Salpekar

Sumit Salpekar, an engineering graduate working as Lead Technical Consultant, is a Sitecore certified professional with 10 years of web development experience and 7 years of experience on Sitecore platform. He likes to explore challenging and trending topics in Sitecore. He worked on numerous Sitecore projects on multiple versions and deliver it successfully.

More from this Author

Follow Us