How to find all Sitecore RichText fields contain scripts inside

One of the customers I was working for recently asked us “Can you find for me all RichText fields that contain scripts and styles tags inside?”. Let’s be honest – in the thousands of items no one could ever done it manually – but with PowerShell you can do that fairly easily.

Here is the script that searches for all fields of “RichText” and then within the usage of indexes tries to find items with these fields containing prohibited values.

$fieldsList = @()
$templatesPaths = @(
    "master:\templates\Branches",
    "master:\templates\Feature",
    "master:\templates\Foundation",
    "master:\templates\Project",
    "master:\templates\User Defined"
    )


ForEach($templatePath in $templatesPaths)
{
    $fieldsList += Get-ChildItem $templatePath -Recurse | Where-Object {
        $_.TemplateID -eq "{455A3E98-A627-4B40-8035-E683A0331AC7}" -and $_.Fields['Type'].Value -eq "Rich Text" -and $_.TemplateID -ne "{0437FEE2-44C9-46A6-ABE9-28858D9FEE8C}" -and $_.TemplateID -ne "{85ADBF5B-E836-4932-A333-FE0F9FA1ED1E}" -and $_.FullPath -notLike "*/System/*" -and $_.FullPath -notLike "*/Experience Accelerator/*" -and $_.FullPath -notLike '*/JSS Experience Accelerator/*' -and $_.FullPath -notLike '*$name*' -and $_.TemplateID -ne "{E269FBB5-3750-427A-9149-7AA950B49301}" -and $_.TemplateID -ne "{A46706F7-EAF8-4575-9860-A85B6F17C5EB}" -and $_.TemplateID -ne "{239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B}" -and $_.Name -ne "__Standard Values"
    }
}

$reportProps = @{
    Title = "Report of RichText fields"
    InfoTitle = "Report of RichText fields containing script and styles inside"
    InfoDescription = "Total entries found: $($itemList.length)"
    PageSize = 25
}

$report = New-Object System.Collections.Generic.List[PSObject]

ForEach ($field in $fieldsList)
{
    Write-Host "Operations on field: "+ $field.FullPath
    $foundPaths = New-Object System.Collections.Generic.List[String]
    $foundIDs = New-Object System.Collections.Generic.List[String]
	
	$foundItems = Find-Item -Index sitecore_master_index -Criteria @{Filter = "Contains"; Field = $field.Name; Value = "script"}, @{Filter = "Contains"; Field = $field.Name; Value = "style"}, @{Filter = "Equals"; Field = "_latestversion"; value = "1"}|Initialize-Item
	ForEach($foundItem in $foundItems)
	{
	    if($foundItem.Fields[$field.Name].Value -like "*<script*" -or $foundItem.Fields[$field.Name].Value -like "*<style*")
	    {
	        $foundPaths.Add($foundItem.FullPath)
	        $foundIDs.Add($foundItem.ID.ToString())
	    }
	}
	#$foundItems| ForEach-Object -Process { $foundPaths.Add($_.FullPath) }
	#$foundItems| ForEach-Object -Process { $foundIDs.Add($_.ID.ToString()) }
	
	$newObject = New-Object PSObject @{__Icon = $field.__Icon; DisplayName = $field.DisplayName; FullPath = $field.FullPath; ID = $field.ID; IDs = ($foundIDs -join "|"); Paths = ($foundPaths -join "|")}
    $report.Add($newObject)
}


$report | Show-ListView @reportProps -Property `
    @{ Label = "Field Name"; Expression = { $_.DisplayName } },
    @{ Label = "Field Path"; Expression = { $_.FullPath} },
    @{ Label = "Field Id"; Expression = { $_.ID } },
    @{ Label = "Items use that field"; Expression = {$_.IDs} },
    @{ Label = "Paths of items using that field"; Expression = { $_.Paths } }
Close-Window

How to find all of the Sitecore Items that use aliases?

Recently I had to find all the items being linked by aliases. Because we had hundreds of aliases and no one wanted to go one by one to check where are they linking to I decided to prepare a PowerShell script to generate a report. Here is the code of it:

$itemList = @()
$allPaths = @(
    "master:\system\Aliases"
    )
    
ForEach($path in $allPaths)
{
    $itemList += Get-ChildItem $path -Recurse | Where-Object {
        $_.TemplateID -eq "{54BCFFB7-8F46-4948-AE74-DA5B6B5AFA86}"
    }
}

$reportProps = @{
    Title = "Aliases Report"
    InfoTitle = "This is a report showing items being linked by aliases"
    InfoDescription = "Total entries found: $($itemList.length)"
    PageSize = 25
}

$report = New-Object System.Collections.Generic.List[PSObject]

ForEach ($item in $itemList)
{    
    $linkedItem = $item.PSFields."Linked item"
    $found = $linkedItem.Value -match '.*id="(.*?)".*'
    $foundItemPath = ""
    $foundID = ""
    if ($found)
    {
        $foundID = $matches[1]
        
        if ($foundItem = Get-Item -Path "master:" -ID $foundID -ErrorAction SilentlyContinue)
        {
            $foundItemPath = $foundItem.FullPath
        }
        else
        {
            $foundItemPath = "Broken Link"
        }
        
    }
    
    $newObject = New-Object PSObject @{__Icon = $item.__Icon; DisplayName = $item.DisplayName; FullPath = $item.FullPath; ID = $item.ID; URL = $linkedItem.Url; LinkedItemPath = $foundItemPath; LinkedItemID = $foundID}
    $report.Add($newObject)
}

$report | Show-ListView @reportProps -Property `
    @{ Label = "Alias Path"; Expression = { $_.FullPath} },
    @{ Label = "Alias Id"; Expression = { $_.ID } },
    @{ Label = "LinkedItem ID"; Expression = { $_.LinkedItemID } },
    @{ Label = "Linked Url"; Expression = { $_.URL } },
    @{ Label = "LinkedItem Path"; Expression = { $_.LinkedItemPath } }
Close-Window

Data migration strategy as a crucial part of Sitecore upgrade

Recently I wrote a blog post describing the challenges of the Sitecore upgrades and now on the Konabos website we released a continuation written by me. I share some knowledge about data migration strategies – you can find it here: Rethinking Sitecore Upgrades: The Art and Science of Content Migration

How to find all unused Sitecore Templates?

Recently one of the customers had a need to identify all of the templates that are not in use. We could go through all of the templates manually and verify all the links but with PowerShell we can do that much faster. Here is a script that will generate a report (that you can later download as Excel).

In this report, you will see all the templates and references – then with the help of Excel filtering you can filter out the rows without any references to get the list of unused templates.

$itemList = @()
$allPaths = @(    
    "master:\templates\Feature",
    "master:\templates\Foundation",
    "master:\templates\Project"
    )

ForEach($path in $allPaths)
{
    $itemList += Get-ChildItem $path -Recurse | Where-Object {
        $_.TemplateID -ne "{0437FEE2-44C9-46A6-ABE9-28858D9FEE8C}" -and $_.TemplateID -ne "{85ADBF5B-E836-4932-A333-FE0F9FA1ED1E}" -and $_.FullPath -notLike "*/System/*" -and $_.FullPath -notLike "*/Experience Accelerator/*" -and $_.FullPath -notLike '*/JSS Experience Accelerator/*' -and $_.FullPath -notLike '*$name*' -and $_.TemplateID -ne "{E269FBB5-3750-427A-9149-7AA950B49301}" -and $_.TemplateID -ne "{455A3E98-A627-4B40-8035-E683A0331AC7}" -and $_.TemplateID -ne "{A46706F7-EAF8-4575-9860-A85B6F17C5EB}" -and $_.TemplateID -ne "{239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B}" -and $_.Name -ne "__Standard Values"
    }
}

$reportProps = @{
    Title = "Templates references"
    InfoTitle = "This report shows all references of the templates"
    InfoDescription = "Total entries found: $($itemList.length)"
    PageSize = 25
}

$report = New-Object System.Collections.Generic.List[PSObject]

ForEach ($item in $itemList)
{
    Write-Host "Referrers for "+ $item.FullPath
    $referrers = Get-ItemReferrer -Item $item
    $referrersPaths = New-Object System.Collections.Generic.List[String]
    $referrers| ForEach-Object -Process { $referrersPaths.Add($_.FullPath) }
    
    $newObject = New-Object PSObject @{__Icon = $item.__Icon; DisplayName = $item.DisplayName; FullPath = $item.FullPath; ID = $item.ID; Status = $status; Referrers = ($referrersPaths -join "|")}
    $report.Add($newObject)
}


$report | Show-ListView @reportProps -Property `
    @{ Label = "Item Name"; Expression = { $_.DisplayName } },
    @{ Label = "Path"; Expression = { $_.FullPath} },
    @{ Label = "Id"; Expression = { $_.ID } }
    @{ Label = "Referrers"; Expression = { $_.Referrers } }
Close-Window

From the list of all paths we exclude things like:

  • folders
  • standard values
  • SXA templates
  • system templates
  • and few more

SQL Server Self-Signed certificate required by Sitecore installer

Recently I had a very interesting case when Sitecore installer was failing during installation because required a secured connection with the SQL Server. It was even more interesting because the connection at the beginning was established correctly, some of the databases were created and then …. during a step related to xConnect and xDB databases I saw an error saying:

A connection was successfully established with the server, but then an error occured during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issues by an authority that is not trusted.)

After some time spent on checking certs generated by Sitecore I realized that I was looking for a solution in the wrong place.

By default SQL Server instance does not use any encryption for incoming connections:

So it should not be a problem – but it was. I decided to add a self-signed certificate to my SQL Server instance to check if it resolved the problem (spoiler alert: it did).

SQL Server requires certificates prepared in a special format. You can generate a certificate with this PowerShell script

New-SelfSignedCertificate -Type SSLServerAuthentication -Subject "CN=$env:COMPUTERNAME" -FriendlyName "SQL Server Self-signed" -DnsName "$env:COMPUTERNAME",'localhost.'  -KeyAlgorithm RSA -KeyLength 2048 -Hash 'SHA256' -TextExtension '2.5.29.37={text}1.3.6.1.5.5.7.3.1' -NotAfter (Get-Date).AddMonths(24) -KeySpec KeyExchange -Provider "Microsoft RSA SChannel Cryptographic Provider"-CertStoreLocation "Cert:\LocalMachine\My"

Details of it are described here:

https://www.sqlserver-dba.com/2022/06/how-to-generate-a-self-signed-certificate-for-sql-server-with-new-selfsignedcertificate.html

When the certificate is generated and added to the certs store you will find it by running mmc (from Windows > Run) where you have to select certificates for your computer.

Now, you have to assign rights to read that certificate for your SQL Server instance.

Open “Services” app and find your “SQL Server” service and check its properties. In the “Log On” tab you will find user name used to run this service:

Now you need to go back to the Certs Store and on your self-signed certificate right-click and select options:

All Tasks > Manage Private Keys

You have to add there a SQL Server user and add it to the list of users allowed to read that key:

I would recommend also copying your key to the “Trusted Root Certification Authorities” folder:


Then export your certificate with a private key using encryption AES256-SHA256:

Ok, at this point you have already exported the certificate ready to be used by your SQL Server.

Again open MMC and this time select SQL Server Manger from the list:

Then go to the “SQL Server Network Configuration” node and right-click protocols to select the properties option:

In the opened window select “Certificate” tab and import the certificate that you have just exported.

Restart your SQL Server instance and your error should be gone now.