Sitecore Box

Sitecore web developer at Americaneagle.com

Triggering an Index Update for the Linked Items in SITECORE






Overview

In some scenarios you may need to include child items into the search document of the parent item. In such cases, if any of the child items is changed (edited\removed) we will have to maintain "relationship" to the linked item (parent item) in the index manually, and this require some coding.

Example

Let’s say we have a computed field attached to the home page, this computed field should contain the value for the “Title” field on “Child Item” and we need that the computed field to be updated once we edit\remove the child item by refreshing the index.





Solutions

Here we have two methods to refresh the index:

1.    indexing.getDependencies Pipeline


This pipeline is designed to address issues when a search document is built from the data coming from more than one item. Each custom processor must implement Sitecore.ContentSearch.Pipelines.GetDependencies BaseProcessor
Below is a sample code how we can implement this:



We need to add the below patch:



2.    Marking the linked item as changed

To mark a linked item as changed, we need just to save it via API (e.g. via custom “item:deleted” handler, that will fire on each delete of the child item). This action will force changing value in the system Revision field, which means that all publishing types will treat this "item change" as the "one that needs to be published". When the parent item is published, it will be handled by the default indexing mechanism, so no further coding is required.

Below is a sample code how we can implement this:


We need to add the below patch:


The same mechanism can be applied on the “item:saved” handler to handle updating the item fields.

References:


SITECORE Multi-Sites with Shared Pages



Overview

SITECORE support having multiple website in a single instance, every site has it is own pages, content, etc...  And you can add shared content items between all sites as you can see in the below screenshot:



That’s awesome! But the challenge comes when you need to have shared pages between more than one sites. Don’t worry it is doable, but we need to do some coding here.



What to do?

I am supposing that you know how to configure multiple website in the normal scenario, if you don’t, please check out this document by Sitecore.

After configuring our sites we need to go through the below steps to have the shared pages working fine:

1)   We need to override “GetItemUrl” method to update the URLs for the items under shared content to be formed the same as the links under the site itself.

We need to be able to browse the pages under the shared folder without include “/Global/” in the URL, for example if we are on “site1” (suppose that the URL is http://site1) and we want to browse this page “/sitecore/content/Global/Products/Product1” then the URL should be this http://site1/Products/Product1 instead of http://site1/Global/Products/Product1 and if we are on “Site2” the URL should be this http://site2/Products/Product1





2)   After updating the URL, now we need to process the request and update Context.Item” value since we don’t have “Products” item under the sites, and this can be done by adding a new processor to resolve such kind of request.

To do that we need to add a new processor in the <httpRequestBegin> to be inherited from HttpRequestProcessor class. In this processor we need to update this property “Context.Item” with the matched item based on the current request.


So in our above example the request will be:

Request: http://site1/Products/Product1

Context item : Sitecore.Context.Database.GetItem( “/sitecore/content/Global/Products/Product1”)



       Don’t forget to add the link provider and the processer to the config using the below              patch:


3)   The last step is make sure that your sitemap.xml is working as expected any update any code login accordingly.


Hide Folder Item Name From URL in Sitecore




In this blog I will explain how we can exclude an item name from the URL programmatically. This can be done by using the out of the box alias feature in Sitecore, so when you create a new item under a specific path, an alias will be created for that item excluding the item you want to hide.


Aliases are based on the System/Alias template and defined under /sitecore/system/Aliases. Each alias name must be unique; the alias template contains a single field in the data section which allows for selection of the target item.

Sitecore determines the default URL for an item based on its path, for example, the URL for /sitecore/content/home/products/product1 is products/product1. Sometimes you need to have a shorter URL which maps to another URL, for example, accessing the above link by this URL /product1; and this what I am going walk you through in this blog.




What we need to do is adding an event on "item:created" to create a new alias whenever we create a new item under “Products” folder as you see in the below screenshot:




Below is the event handler for "item:created":


We need to create a new config file and add it the App_config folder:


Find the item's languages Programmatically in Sitecore




Everybody knows how to find the languages for any item in the Sitecore tree. This can be done by selecting the item and clicking on the language dropdown on the right side as you can see in the below screenshot.




During my work, I needed to find all items with their version languages so I created MVC view to show them as an HTML table.



Sitecore from headless CMS Perspective


What is a Headless CMS?

Headless CMS simply is a separation between the content and presentation. 

The regular CMS gives an ability to store and manipulate the data, create, update and delete and gives ways to view the data, while the headless CMS gives the same things but, instead of viewing the data gives an API to access the data and make the presentation regardless of what the channel you are using to present the data.

In the headless CMS the developers can write the code and prepare the front-end design in their preferred language without having to be restricted in back-end technologies. They can use Application Programming Interfaces (APIs) to consume the back-end functions in any front-end environment like webpages, smartphones, wearables, headsets, etc...



Image credit for this site https://medium.com/tech-tajawal/why-headless-cms-is-becoming-so-popular-57d262b1e096

Sitecore as a Hybrid Solution

Sitecore support both approaches, a site generated with the Sitecore layout engine or a headless CMS and app based on the Sitecore REST API (JSON), and from here we can realize how Sitecore is unique in this aspect.

When Sitecore® Experience Platform™ (XP) 7.5 launched, they introduced the Sitecore Services Client, which is an API layer that uses RESTful services to communicate with channels. When a request comes, Sitecore translates the request to a search query which in turn returns the information to the device.

So the users can consume the content that comes from Sitecore Services Client in different channels, from mobile applications to any IoT-connected device.

Sitecore JavaScript Services (JSS), is a good example of headless solution that allows developers to build solutions using Sitecore and modern JavaScript UI libraries and frameworks Angular, React, and Vue.js to render Sitecore content on any device without the need for Sitecore prerequisites.


Headless CMS Downside

The downside is that your app’s customer experience will also be decoupled. You won’t be able to personalize that experience, or respond in real time with relevant content, or test and optimize and manage forms and market in context of user interactions.

Your content may live on the devices but you can’t collect the user’s interactions and do personalized content.

The good news is in Sitecore you can transform the content into JSON format by dragging and dropping JSON components on the page using Sitecore® Experience Accelerator as well as JSS personalization works exactly how it does in a non-headless Sitecore site. The Layout Service renders pages using the exact same pathway as a traditional Sitecore site uses - meaning that personalization rules, multivariate tests, etc are all respected. As a developer, there's nothing you have to do. The content returned to your JSS app is pre-personalized before it is received.


Media Request Protection and Security



Introduction

Media request protection feature introduced by Sitecore in version 7.5 to restricts media URLs that contain dynamic image-scaling parameters so that only server-generated requests are processed. This ensures that the server only spends resources and disk space on valid image scaling requests.

You can turn on/off this feature by updating the value for this setting item :


When the feature is enabled, Sitecore automatically signs image URLs that are rendered by the pipeline and adds a hash value to the query string. When processing an incoming media request, image resizing/scaling is skipped if any of the relevant query string parameters in the image URL have been altered or any extra resizing parameters have been appended to the URL. In these cases, Sitecore returns the original, unaltered image. Requests for the original image (without any resizing/scaling parameters) work as usual and are not restricted.

Turning off this feature may cause a high disk space consumption so be careful when disabling this feature.

Known Issue Error

Using the media items inside the Rich text editor field and render the field using field value directly " ex : @Html.Raw(item.Fields["Body"].ToString())" does not process its value through Sitecore's field rendering pipelines. This prevents expansion of links, generating hash and other dynamic features. In this case, Sitecore would not validate whether the media link was rendered by Sitecore or was modified by a visitor and may cause a lot of error messages inside log files such as the below one:


"MediaRequestProtection: An invalid/missing hash value was encountered The expected hash value: ............."

Solution

To resolve this issue, please consider using Sitecore's MVC helper method to generate markup of a field: @Html.Sitecore().Field("Body", item)

Sitecore 9 On Premises installation step by step using SIF


 


Introduction

In this blog I will be talking about Sitecore 9 XP Single (XP0) On Premises installation step by step using SIF (Sitecore Install Framework).


SIF

The Sitecore Installation Framework is a Microsoft® Powershell module that supports local and remote installations of Sitecore, and it is fully extensible. You can install the entire Sitecore solution (XP), or the CMS-only mode (XM) solution.

Prerequisites

·        Web platform installerUse this for installing the following software:
-      Web Deploy 3.6 for Hosting Servers.
-      URL Rewrite 2.1 (Use Web Platform Installer to install).


·        We need to have Java runtime installed

-      Click here and accept the license then choose your platform.



-      Go to advanced system settings by writing ‘advanced’in windows search.



-      Click ‘Environment Variables’button and add ‘JAVA_HOME’to the system variable as you can see below:



-      Scroll down in System variables area and select path then click edit.
-      A new window will appear, click new and enter ‘%JAVA_HOME%\bin’ as you can see below:



-      Click ‘OK’ for all opened windows.
-      To make sure that Java installed correctly, open cmd and write the below:
java –version


Installation with Microsoft PowerShell®

       SIF is available using through Sitecore Gallery, to install MyGet repository, in Windows, launch Microsoft PowerShell® as an administrator and run the following cmdlet:

         Register-PSRepository -Name SitecoreGallery -SourceLocation                  https://sitecore.myget.org/F/sc-powershell/api/v2

-   Enter ‘Y’ and click enter.

-   Install the PowerShell module by running the following cmdlet:
          Install-Module SitecoreInstallFramework

-   Enter ‘Y’ and click enter.

-   Install the Sitecore Fundamentals module by running the following cmdlet:
Install-Module SitecoreFundamentals

-   Import the modules into your current PowerShell context by running the following cmdlet:
Import-Module SitecoreFundamentals
Import-Module SitecoreInstallFramework


Install Solr

-   We need to install Solr 6.6.2, to download this click here. Make sure to extract the .zip file on the following path:
C:\solr-6.6.2

-   To complete the setup for Solr as https, we need to install OpenSSL, click here and install it.

Enabling SSL

1.    Generate a Self-Signed Certificate and a Key
-   Create new folder under Solr path "C:\solr-6.6.2\bin\etc" called ‘etc’.
-   To generate a self-signed certificate and a single key, open windows command prompt (cmd) as an administrator and go to etc folder you just created (Write > cd C:\solr-6.6.2\bin\etc ) and run the below commands:

keytool -genkeypair -alias solr-ssl -keyalg RSA -keysize 2048 -keypass 123456  -storepass 123456 -validity 9999 -keystore solr-ssl.keystore.jks -ext SAN=DNS:localhost,IP:127.0.0.1 -dname "CN=localhost, OU=Organizational Unit, O=Organization, L=Location, ST=State, C=Country"

2.    Convert the Certificate and Key to PEM Format for Use with cURL
-   First convert the JKS keystore into PKCS12 format using keytool (in cmd):
keytool -importkeystore -srckeystore solr-ssl.keystore.jks -destkeystore solr-ssl.keystore.p12 -srcstoretype jks -deststoretype pkcs12
12

-   cmd will ask you to enter passwords, please enter 123456 as we used this in generating self-signed certificate above.

-   Next convert the PKCS12 format keystore, including both the certificate and the key, into PEM format using the OpenSSL command (in cmd):

C:\OpenSSL-Win64\bin\openssl pkcs12 -in solr-ssl.keystore.p12 -out solr-ssl.pem

-   Close cmd and go to the below path to installed the certificate by selecting ‘solr-ssl.keystore.p12’ and right click and choose install.




3.    Set Common SSL-Related System Properties

-   To activate the SSL settings, open this file ‘C:\solr-6.6.2\bin\solr.in.cmd’and uncomment and update the set of properties beginning with SOLR_SSL as you can see below:



-   Now we need to run Solr as a Windows service, to do so we need do the following:

1.    Download NSSM. And extract it to (C:\nssm-2.24\win64).

2.    Open command prompt as an administrator and go to the above folder, enter (> cd C:\nssm-2.24\win64).

3.    Install Solr by writing (nssm  install solr-6.2.2) in cmd.

4.    The below window will appear:



5.    Select the path and the startup directory for Solr as you see in the above screenshot and click install service button.

6.    Go to windows services and start Solr service






4.    Run Single Node Solr using SSL

1.    Save the below Powershell script in a file (ex: C:\Sitecore\Sitecore 9\ Solr-SSL.ps1).


Make sure to change the password in the parameter section:

[string]$KeystorePassword = '123456',

And make sure to update (keytool) path to the correct one:

$keytool = (Get-Command 'C:\Program Files\Java\jre1.8.0_171\bin\keytool.exe').Source

2.    Open Powershell as an administrator and run this script.

·        Go the script path and enter the below and click enter.
.\Solr-SSL.ps1 -keystorefile C:\solr-6.6.2\server\etc\solr-ssl.keystore.jks





3.    Restart the Solr windows service.

4.    Make sure that the Solr is running using SSL by opening this URL https://localhost:8983/solr





Sitecore 9 installing packages

Go and install packages for XP Single (XP0) Instance configuration. Download packages for XP Single.



Copy the installation file and extract it to C:\Sitecore\Sitecore 9

Extract to the configuration file into the same path

Copy Sitecore license (license.xml) file to the same folder.





Make sure to update Solr paths in the below files:
-   sitecore-solr.json
-    xconnect-solr.json

Create a Powershell script for the installation and copy the below inside it on the same folder and name it ‘Installps.ps1’.


Make sure to put the correct values in the below variables:

-   Root file ($FilesRoot = "C:\Sitecore\Sitecore 9").
-   $SqlServer = "localhost"
-   $SqlAdminUser = "sa"
-   $SqlAdminPassword = "12345"
-   Package = "$FilesRoot\Sitecore 9.0.0 rev. 171002 (OnPrem)_single.scwdp.zip"
-  Package = "$FilesRoot\Sitecore 9.0.1 rev. 171219 (OnPrem)_xp0xconnect.scwdp.zip"


This script will create a DB user with the same name exists in this variable ‘$SqlAccountUser = "$prefix" which is in our example ‘sc900’, so please make sure to put a password on this variable $SqlAccountPassword = "Admin123" meets the password policy for your SQL server instance.


Open Windows PowerShell ISE as an administrator and open the script we created (Installps.ps1) and run the script.


If any error occurred during the installation, try to fix the error and remove the databases which are created during the installation, then re-run the script again.


Congrats! The installation is done, go and enjoy Sitecore 9.