Part 1: Getting started with Sitecore CLI

Series of Sitecore CLI blogs

Following are the steps to install and use Sitecore CLI for your project:

Pre requisites:

Install  .NET Core 

Sitecore Instance

Working Sitecore identity

Steps to install:

  • Install Sitecore Management services package(few dlls and configs) on your sitecore instance. Make sure to check compatibility from here.
  • Open Powershell in admin mode
  • Run following commands in your root directory:

> dotnet new tool-manifest
> dotnet nuget add source -n Sitecore https://sitecore.myget.org/F/sc-packages/api/v3/index.json
> dotnet tool install Sitecore.CLI 
Use the -g option when running the install command to install CLI globally. However, it is not recommended because different instances may need different version of Sitecore CLI
  • Initialize a new project
> dotnet sitecore init
  • Install the required Publishing and Serialization plugins
> dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Serialization
> dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Publishing

You can check the list of plugins installed by using following command:

> dotnet sitecore plugin list

After this, your folder will look like following:

To verify the sitecore CLI is successfully installed and working(-h for help)

> dotnet sitecore -h

Login to Sitecore with Sitecore CLI:

Sitecore CLI supports two flows of authentication and authorization.

  • An interactive user login, using a device code flow. Please follow below steps for interactive login:
> dotnet sitecore login --authority https://<sitecore-identity-server> --cm https://<sitecore-instance> --allow-write true

This will navigate you to browser to authorize and modify ~/.sitecore/user.json with access tokens. Make sure not to commit user.json as it contains sensitive information. After this, you are good to serialize items.

  • A non-interactive client login, using a client credentials flow. This is used by clients such as Continuous Integration servers. Please follow below steps for non- interactive login:

Make configuration changes:

create a file named Sitecore.IdentityServer.DevEx.xml and add following:

<?xml version="1.0" encoding="utf-8"?>
<Settings>
  <Sitecore>
    <IdentityServer>
      <Clients>
        <!-- used to authenticate servers with client id and client secret -->
        <CliServerClient>
            <ClientId>SitecoreCLIServer</ClientId>
            <ClientName>SitecoreCLIServer</ClientName>
            <AccessTokenType>0</AccessTokenType>
            <AccessTokenLifetimeInSeconds>3600</AccessTokenLifetimeInSeconds>
            <IdentityTokenLifetimeInSeconds>3600</IdentityTokenLifetimeInSeconds>
            <RequireClientSecret>true</RequireClientSecret>
            <AllowOfflineAccess>false</AllowOfflineAccess>
            <AllowedGrantTypes>
                <!--
                    client_credentials authenticates with client ID and client secret
                    which is good for CI, tools, etc. However, it's not tied to a USER,
                    it's tied to a client ID.
                -->
                <AllowedGrantType1>client_credentials</AllowedGrantType1>
            </AllowedGrantTypes>
            <ClientSecrets>
                <!--<ClientSecret1>SUPERLONGSECRETHERE</ClientSecret1>-->
            </ClientSecrets>
            <AllowedScopes>
                <!-- this is required even if not a 'user' for Sitecore to like us -->
                <AllowedScope1>sitecore.profile.api</AllowedScope1>
            </AllowedScopes>
        </CliServerClient>
      </Clients>
    </IdentityServer>
  </Sitecore>
</Settings>

Add client id and client secret(max length = 100) in this file. Keep this file in Config folder of Sitecore Identity Server.

create another file Sitecore.Owin.Authentication.ClientCredentialsMapping.
config
 containing the following:

<?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">
                <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" />
				<claim name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" value="" />

              </targets>
              <keepSource>true</keepSource>
            </transformation>
          </transformations>
          
        </identityProvider>
      </identityProviders>
    </federatedAuthentication>
  </sitecore>
</configuration>

Add email id(same client id as first file) in email address claim value. And keep it in App_Config/Include/ folder of Sitecore Content management server.

Do the IIS Reset.

Open powershell in admin mode.

Now login:

> dotnet sitecore login --authority https://<sitecore-identity-server> --cm http://<sitecore-instance> --allow-write true --client-credentials true --client-id <client-id> --client-secret <client-secret>

Login information will be saved in .sitecore/user.json

Next step would be Sitecore serialization configuration- structure, rules and items pulled depends decisions taken in defining the configurations.

How you want the items to be pulled? you want to opt for site wise folders, component wise folders, page wise folders, etc. What fields should be excluded while performing pull and push of items?

Everything depends on how you plan to do Sitecore serialization configuration. Please find the details in next blog.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: