Pages

22 March, 2022

Sitecore CLI - Authentication and Authorization

Sitecore has a very good documentation on Authentication and Authorization. As stated, there are two flows. 
  1. Interactive User Login
    • Sitecore Username and Password are needed. 
    • Used mostly by the developers.
  2. Non-Interactive Client Login
    • Client ID and Client Secret are needed. 
    • Client should be configured in Identity server and Identity provider should be configured in Content Management as documented
    • Used for CI/CD process to automate tasks.
In both the flows,  
  • Identity server is must. 
  • The result of the authentication and authorization is access token. If we wanted extended expiry of tokens for a longer running process, we can opt for refresh token. 
When we initiate the interactive login request using dotnet sitecore login --authority https://id.sitecorewarriors.localhost/ --cm https://cm.sitecorewarriors.localhost --allow-write true, Sitecore ID login screen will appear. Once logged in, we will get two options. 


Offline Access will generate refresh token along with short lived access token. A refresh token is a credential artifact that lets a client application get new access tokens without having to ask the user to log in again.

.sitecore/user.json file will be populated with the access token and refresh token. So we should not commit the .sitecore/user.json file to source control as it contains sensitive information.


21 March, 2022

Sitecore CLI Extension Development - Possible errors and solution

While we (Sitecore Warriors) were working on creating Sitecore CLI extension in Sitecore Hackathon, we came across few errors and I would like to share it in this article. 

  • Could not locate plugin SitecoreWarriors.DevEx.Extensibility.Jobs@4.1.1. Some CLI commands may not function correctly.

    To speed up the development and testing the plugin, we can manually place the plugin assembly in /.sitecore/package-cache/nuget/PluginName and add the plugin name in the sitecore.json file. 


    Possible Reason 1: If the assembly name and plugin name do not match, then we will get this error. Once we set the same name for plugin as well the assembly name, the dotnet sitecore -h command will list the plugin. 

    Possible Reason 2: If the nuget package or the plugin assembly is not placed inside a folder called plugin, you will get this error. 


    PS C:\Project\Working\CLI> dotnet sitecore -h
    >> Begin installing NuGet packages: SitecoreWarriors.DevEx.Extensibility.Jobs@4.1.1
    >>> Skipping NuGet package SitecoreWarriors.DevEx.Extensibility.Jobs@4.1.1 because it is already installed.
    Could not locate plugin SitecoreWarriors.DevEx.Extensibility.Jobs@4.1.1. Some CLI commands may not function correctly.

  • Plugin does not supported by the current version of the CLI. Please try to update you CLI

    Plugin product should be .NET Core project. If the plugin assembly project is created as a Web SDK (Microsoft.NET.Sdk.Web) project instead of just an SDK project, CLI will not be able to recognize the plugin. Make sure the project type is Microsoft.NET.Sdk


15 March, 2022

Job Plugin - Sitecore CLI Plugin - Sitecore Hackathon 2022

Sitecore Hackathon is a community driven event and we (Sitecore Warriors) chose idea Extend Sitecore CLI.

We chose many ideas and finally ended up completing Job related plugin. 

This Sitecore Job plugin will enable developers/admins to
  1. View currently running, queued and finished jobs. Also lists configured Sitecore Database jobs.
  2. Execute Sitecore Database jobs on-demand
  3. Rebuild the Link database
There are two parts in developing a Sitecore CLI Plugin. 
  1. Plugin (published in nuget.org)
  2. Sitecore Management Service to initiate the action in Sitecore Context.

Installation instructions:
  1. Sitecore instance (10.2) with Sitecore Management Services package installed. Setup a project folder in your workstation. Install and initiate the Sitecore CLI as mentioned in this document
  2. As part of this job plugin, we have created a management service which can receive the request from the CLI and execute the action in Sitecore. The management service package is placed here. This package can be installed using Sitecore Installation Wizard. 
  3. To install the job plugin, Sitecore has provided a plugin called plugin. You can pass on the plugin name along with add subcommand. Please note that this plugin is already published to nuget.org here
    dotnet sitecore plugin add -n SitecoreWarriors.DevEx.Extensibility.Jobs
  4. To verify whether job plugin has been installed successfully, you can run dotnet sitecore job -h
Usage:
dotnet sitecore job [subcommand] [options]
Subcommands:

You can use the following subcommands:
  1. list - Get all jobs list (running, queued, finished and db task jobs). Db task can be started on-demand.
  2. rebuildlinkdb - Start rebuilding a link db.
  3. start - Start a DB task.
Options:

You can use the following options with the list subcommand:

Option Explanation
-c, --config (config) Path to root sitecore.json directory (default: cwd)
-v, --verbose Write some additional diagnostic and performance data
-t, --trace Write more additional diagnostic and performance data
-?, -h, --help Show help and usage information

You can use the following options with the start subcommand:

Option Explanation
-c, --config (config) Path to root sitecore.json directory (default: cwd)
-j, --job-name (job-name) Mention DB Task Schedule Name from Listing.
-v, --verbose Write some additional diagnostic and performance data
-t, --trace Write more additional diagnostic and performance data
-?, -h, --help Show help and usage information

You can use the following options with the rebuildlinkdb subcommand:

Option Explanation
-c, --config (config) Path to root sitecore.json directory (default: cwd)
-db, --database (database) Mention DB name for rebuilding the link DB (default: master)
-v, --verbose Write some additional diagnostic and performance data
-t, --trace Write more additional diagnostic and performance data
-?, -h, --help Show help and usage information

Demo:

dotnet sitecore login
  • Install the job plugin.  
    dotnet sitecore plugin add -n SitecoreWarriors.DevEx.Extensibility.Jobs


  • Get the list of commands in job plugin.  
    dotnet sitecore job -h


  • To view the list of running, queued, finished and DB tasks.  
    dotnet sitecore job list


  • To start a DB task. The last run changes once the job gets executed.
    dotnet sitecore job start -j {JobName}


  • To rebuild the link database.
    dotnet sitecore job rebuildlinkdb -db master


Sitecore CLI is a powerful tool to automate admin tasks and integrate it as part of DevOps. Since additional plugins can be added easily, this tool can integrate not only with Sitecore XM/XP, it can be leveraged for other Sitecore product family with few customization. 

14 March, 2022

Our Sitecore CLI Extension Ideas - Sitecore Hackathon 2022

Sitecore Hackathon is a community driven event and for this year 2022, the ideas were

  1. Build an e-commerce Minimum Viable Product to sell community t-shirts 
    • The submission must include the following technologies: 
      1. Sitecore XM - Rendering host (.net or jss)
      2. Sitecore Send
      3. Sitecore Order Cloud
  2. Extend the Sitecore Command Line Interface (CLI) plugin 
    • https://doc.sitecore.com/xp/en/developers/102/developer-tools/sitecore-command-line-interface.html
  3. Best addition to the Sitecore MVP site 
    • https://github.com/Sitecore/MVP-Site

We, Sitecore Warriors, chose Idea 2 - Extend Sitecore CLI Plugin. 

We came up with multiple ideas to create a plugin as a Dev Collection. Our ideas were

  1. View Running, Queued and Finished jobs
  2. Rebuild Link Database
  3. Start a Sitecore DB Task
  4. Run a PowerShell Report and export an excel
  5. Cache Tools
    1. View Statistics
    2. Clear Cache
  6. View ShowConfig of CD environment
  7. View Sitecore System Performance
  8. Run an Audit to find any commonly known vulnerabilities
  9. Create Anti-Package
  10. View License Details 
Finally we ended up creating only top 3 items in the above list. 

Our submission link: https://github.com/Sitecore-Hackathon/2022-Sitecore-Warriors. Unfortunately we did not allocate time to record a video properly (😉)

Forked: https://github.com/nehemiahj/2022-Sitecore-Warriors


blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }