Sitecore CLI Setup Guide for Sitecore Application

Below is the recommendation from Sitecore Before Starting Setup CLI for your Sitecore application.

Below are steps to set up Sitecore CLI for your Sitecore Application.

1)    Reference: Sitecore Management Services

Sitecore Management Services is a package that you must download and install in your Sitecore Content Management (CM) instance to support Sitecore Command Line Interface

The download page: Sitecore Downloads: Sitecore CLI 300

From the download page, please download the highlighted package

Note: Always install an updated version of the Sitecore CLI module into your Sitecore CMS,

Because the updated version is supporting the Lower version of Sitecore XP versions. As shown below image yellow highlighted link, click on that and you can find the latest Sitecore CLI module.

2) Install Sitecore CLI module Package on your Sitecore CM Instance.

3) To install Sitecore CLI for your Project, to follow below steps.

  a) Open Power Shell / CMD with administrator privileges.

b)  Go to Project Path from Command Line

cd <project folder>

c) Run the following command

i)dotnet new tool-manifest
ii) dotnet nuget add source -n Sitecore https://sitecore.myget.org/F/sc- packages/api/v3/index.json
iii) dotnet tool install Sitecore.CLI 
iv) To initialize your new project, run the following command in your project folder:
   dotnet sitecore init
v) 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
 
vi)  To check the installed plugins
dotnet sitecore plugin list
 
4) Version Control –Serialized Content

You will see the module definitions and serialized items

a)  Need to create the Module.Json file, which is having specific Sitecore Layer information, which we want to serialize for respective project.




    

You can see the path configured for serialization Item of CMS into sitecore.json file 




 

5) CLI Initialization and connect  Sitecore Application(Visual Studio ) to Local Sitecore CMS instance.

  • Open PowerShell
  • Switch the directory to your solution folder. (for ex: D:\Sonic\repo)
  • Use the below command to login into your CM instance.

dotnet sitecore login –authority <<identityserverurl> –cm <<cm instance url>>  –allow-write true

for ex: identityServerUrl = https://sc10u1identityserver.dev.local

CM = https://sc10u1sc.dev.local

  • It opens browser for authentication.
  • Once the login is successful, click on the “Yes Allow” button on the permission window.
  • Come back to your PowerShell window once the connection is established

After authentication, user.json file will create, which having info about Sitecore instance and access token, using that one to utilize the pull/push and watch command for that particular Sitecore instance.

6) Pull items from your local CM instance into Visual Studio Application.

 

Run the below command

dotnet sitecore ser pull

This will pull all the changes from local CM instance to your file system.

7) Push items to your local instance

Run the below command

dotnet sitecore ser push

This will push all the changes from file system to your local instance.

8) Enable Watch

Run the below command to enable transparent sync

dotnet sitecore ser watch

You must run this command before you start making any changes to your items in your local instance. This will automatically serialize your items to file system.    

9) Check-in you changes

Check-in your changes.

This will also check-in your sitecore definition items along with content and data source items that you have updated on your local instance.

Key Notes

  • Make sure your CM and Identity Server instances are up and running
  • Package your items before you run the synchronization. The initial content synchronization overrides your changes.
  • Take the latest every time and push the content changes into your local instance. This way you can keep your local instance updated.
  • Enable watch before you start making any changes
  • Please do not check-in your local items that are not in use or not needed for any other developers. For example, the sample items that you have created for exploring something.
  • Double check the sitecore definition items before you check-in.

Sitecore Reference Links :

https://doc.sitecore.com/xp/en/developers/100/developer-tools/install-sitecore-command-line-interface.html

Issue Links:

https://sitecore.stackexchange.com/questions/26004/error-message-make-sure-the-graphql-service-is-installed-and-available

 
 Happy Sitecoreing 😊
 
 

Create the SRI(Sub Resource Integrity for Javascript Library/CDN URL)

  1. For CDN JavaScript URL , you can directly visit on this URL(https://www.srihash.org/) and paste your CDN URL(e.g.https://example.keycdn.com/path/example.js ), It will generate the Hash Key. Need to put the Hash key with append the algo key e.g. integrity=”sha384-xnSV7Ujm6p40d9oLBjPsEZVraTaUO5LOWBOcat4SO3W8JI/jp7QXU5Be9/iy5bfE” crossorigin=”anonymous”, “sha384” is SRI hash key.
  2. For those which is not serving from CDN, in that case you need to download the openssl.
    • a) Open CMD as administrator mode and come to the path where openssl .exe is placed.
    • b) Download the required the JS file from that path or create the file.
    • c) Execute the command :
    • openssl dgst -sha384 -binary FILENAME.js | openssl base64 -A
    • Into the cmd at the place where the openssl .exe exist.
  3. For more reference you can visit mentioned url: https://www.keycdn.com/support/subresource-integrity

Install / Uninstall SitecoreInstallFramework(SIF)

If your machine has already installed SIF e.g. On your machine have 2.2.0 version of SIF , which is compatible for installation of Sitecore 9.3, while you want to install Sitecore 10.x on your machine than SIF showing message you need to uninstall SIF which is already exist and install SIF version 2.3.0, which is compatible to Install the Sitecore 10.x on your Machine.

Please find below the Uninstall and Install Powershell command for SIF.

Uninstall All Version: Uninstall-Module -Name SitecoreInstallFramework -AllVersion

List of Installed All Version: Get-Module SitecoreInstallFramework -ListAvailable

Install Required Version SIF: Install-Module -Name SitecoreInstallFramework -RequiredVersion 2.3.0

Uninstall Required Version: Uninstall-Module -Name SitecoreInstallFramework -RequiredVersion 2.3.0

Happy Coding 🙂

Create Certificate from Powershell script

Please find the below script which are used to generate the certificate, point to remember is that domain name should be match which are present in host file and also match with your iis. e.g. “local.demo.com“.

$cert = New-SelfSignedCertificate -certstorelocation cert:\localmachine\my\ -dnsname local.demo.com
$pwd = ConvertTo-SecureString -String ‘Test11‘ -Force -AsPlainText
$path = ‘cert:\localMachine\my\’ + $cert.thumbprint
Export-PfxCertificate -cert $path -FilePath C:\certificates\local.demo.com.pfx -Password $pwd
$cert.thumbprint

$rootStore = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList Root, LocalMachine
$rootStore.Open(“MaxAllowed”)
$rootStore.Add($cert)
$rootStore.Close()

Create and Use Goal

In this Article we will cover, What is the goal?, Why use goal? , How to create and use goal in Sitecore.

What is the goal?

A goal in Sitecore is any action that a user completed. It can be a page that has been viewed, a form that has been completed or other actions.

Why use the goal?

Goals are used to track and measure how visitors engage with the website and campaigns. Engagement value points use to measure relative contact engagement across your website.

You can use engagement value points to

=>Create and implement personalization rules for contacts

=>Create marketing automation campaigns that evaluate the number of engagement value points that contact has achieved to determine what action should be taken.

The Engagement Value Scale looks like as shown below image:

EngagementValueScale

How to create and use goal in Sitecore.

1) Default Goals:-> Those goals which are default creates under Marketing Control Panel=>Goals are called default goals

e.g. “Download Broucher”,” Login”, “Register” etc.

Use Default Goals:-> In my case, I used “Download Broucher” goal on the media item, as shown below.MediaItemGoal

Click on Analyze tab=>Click on Goals and select “Download brochure” after that save and publish for applying this goal.

2) Custom Goals:->Creating new Goals under System=>Marketing Control Panel=> Goals, are called custom Goals.

E.g. I created goal, which name is “Read More Goal” after that click on “deploy action” can be found on the review tab,

for showing the goal in the list of rule during applying the goal on presentation rendering of an item.Deploy Goal

How to Trigger a Goal

Method 1: Trigger a Goal using Query Parameter This method uses the “sc_trk” query parameter to trigger the goal

In Sitecore, if your anchor link used “General Link” URL, click on

“Insert link” as shown belowGenerallink_Querystring

After that assign query string value like sc_trk=[goal name]or [goal itemID], as shown belowApply

Method 2: Trigger a Goal using Goal Item ID / Item Name (Preferable Way) This method uses the item ID of the goal to trigger because of Goals Ids in Sitecore do not change.

If the location of the goal is changed or if the name of the goal is changed.

Ajax call for Api Method:

$(‘.personalisegoal’).click(function(){

var url = “/api/sitecore/API/AnalyticsEvent/”;

var Obj = {};

Obj.ItemId=”0526CFBE-9547-42C0-8410-914BE47B73E1”;

var objPageEventModel=JSON.stringify({ ‘pageEventModel’: Obj });

$.ajax({

contentType: ‘application/json; charset=utf-8’,

dataType: ‘json’,

url: url,

data: objPageEventModel,

cache: false,

type: “POST”,

success: function(response) {

console.log(response);

}

,

error: function(response) {

console.log(“error”, response);

}

});

Api controller

public JsonResult AnalyticsEvent(PageGoalModel pageGoalModel)
{
try
{
Sitecore.Data.ID suzukiConnnectGoal = new Sitecore.Data.ID(pageGoalModel. ItemId);
RegisterEventDataService.TriggerGoal(suzukiConnnectGoal);

}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(“Error in SaveEMICalculatorAnalytics Method , Item Path : ” + Sitecore.Context.Site.StartPath + “”, ex, this);
}
return Json(true, JsonRequestBehavior.AllowGet);
}

Service Code

public static void TriggerGoal(Sitecore.Data.ID goalId)
{
if (!Tracker.IsActive)
{
Tracker.StartTracking();
}
if (Tracker.IsActive && Tracker.Current.CurrentPage != null)
{
Sitecore.Data.Items.Item goalItem = Sitecore.Context.Database.GetItem(goalId);
if (goalItem != null)
{
var goalTrigger = Tracker.MarketingDefinitions.Goals[goalId.ToGuid()];
var goalEventData = Tracker.Current.CurrentPage.RegisterGoal(goalTrigger);
goalEventData.Data = goalItem[“Name”];
goalEventData.ItemId = goalItem.ID.ToGuid();
goalEventData.DataKey = goalItem.Paths.Path;
goalEventData.Text = goalItem[“Name”];
Tracker.Current.Interaction.AcceptModifications();
}
}
}

Where we can apply all created goals?

On the item of the content tree, we will apply the created goals.

First select an item “About Habitat” from the content tree, after that click on “Presentation”=> “Detail ”=>Click on “Personalise tab” of selected Rendering. As shown below.AllRendering

Image: Showing All RenderingSelect Rendering and Applying Goal

Image: Select Rendering and Applying GoalChange the Datasource after the meet Goals

Image: Change the Datasource after the meet Goals

Happy Coding 🙂

Rendering Parameters

What is the rendering parameter? Rendering parameter is a way to pass additional items or values on the presentation component rendering, apart from rendering data source.
When we use a rendering parameter? It is a situation where content editors need additional items apart from its actual data source.

For Example, I used the Rendering Parameter to pass and additional CSS class to the component, which will allow my content editors to choose the style of their component.

How to use the rendering parameter? There are three step activity to create and consume a rendering parameter.

1)      Create Rendering Parameter Template.

2)      Assign a Rendering Parameter to a respective component.

3)      Retrieve or utilize the Rendering Parameter value in the code.

  • Create Rendering Parameter Template.

Create a new parameter Rendering template make sure creating template should inherit from “Standard Rendering Template” as shown below in the image.

Create Rendering Paramter Template

Image: creating a rendering parameter template.

After creation template, Create the field as shown below in the image.

FieldCreation Rendering Parameter

2) Assign a Rendering Parameter to a respective component.

Assign you parameter template on your required rendering under the Editor Options section of your rendering, as shown below

Assign Rendering Template at Rendering

After referencing your Rendering Template to the respective rendering, as shown above in the image, check the same by going to the content item where you are using rendering parameter go to the

Presentation Details=> Final Layout

And select your rendering and click on edit – you can see your Rendering  Parameter field there. As shown below.

Editor Assign Value at Rendering Parameter

 

 3) Retrieve or utilize the Rendering Parameter value in the code.

  1. a) Read the rendering parameter value at the partial view.

var rc = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull;

string styleName = string.Empty;

if (rc != null)

{

var parms = rc.Rendering.Parameters;

styleName = parms[“Css Name”];

}

I hope this post will help you in understanding the Rendering Parameter.

Happy Coding 🙂