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

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

How to add https/ssl support to Sitecore 9.1 local instance?

I noticed that accidentally that by default CM application uses http not https – at least when we use installation scripts for on-premise XP0 topology.

Because I wanted to add https to my website I asked myself – “how can I do that EASILY?” I do not know how about you – but I hate to play with certificates on my local machine – very often it means many lost hours which I could spend on development of new features.

To achieve that I checked how Sitecore create and install certificates inside their scripts and I found the following task:

 Invoke-AddWebFeatureSSLTask

It took me few minutes to find final list of attributes which should be set to add certificate acceptable by services like identity server or xconnect. Final version is here:

Invoke-AddWebFeatureSSLTask -Hostname sc910.sc -SiteName sc910.sc -Port 443 -ClientCertLocation LocalMachine -OutputDirectory "C:\certificates" -RootDnsName "DO_NOT_TRUST_SitecoreRootCert" -RootCertName "root-authority"

HostName and SiteName should be values used by your website (check it in IIS configuration if you are not sure). Output directory is a place where you store your certificates. The rest of the parameters I would leave unchanged because they use default values from Sitecore’s installation scripts.

As a result of this task you should see:

  • new certificate file
  • new certificate registered in IIS
  • new binding added to your website

Because Identity Server uses http by default – please remember to update its configuration. If you do not do that – you will not be able to log in into Sitecore instance.
More details about that you will find here.

PowerShell – Stage 2

In this part of PowerShell topic I will show you how to create IIS website and unzip Sitecore into the new site directory.

Set site name

Before we will do a real job we have to define name of our new site. We can ask user in console to set the value. This code will prompt for name:

As result we will have name saved in variable $nameProvidedByUser.

Stop IIS

Because we have to add new ApplicationPool, create new IIS Site, sometimes also override some values in confiugration – it is better to stop our local instance of IIS before we will start.

We can do this with script:

After that definition we have to just call our function with command “IISStop”

Do some real work – define/copy/run

Ok, for now we have defined name and stopped IIS instance. We are ready to do some real work.

Run following command and at the end you will have ready to work IIS site and copied Sitecore files inside web root.

We have here code for:

  • unpacking ZIP files
  • changing the structure of unpacked files
  • creation of IIS AppPool
  • creation of IIS Site
  • creation of settings for bindings


But what exactly this command will do? Here you have explanation.

Inside this block of code we use some functions which were prepared before.

Add host to Windows file

If we want to define new site in our environment we also should add new definition in windows hosts file. We can do this with following script:

Start IIS

Because we have in plan to stop our IIS we also should have a script to start it again.

We can do this with following function:

Select version of Sitecore

Probably you will have more than one version of Sitecore in your files system – it means that would be nice to have an option to select which version of Sitecore should be copied into our web root. We can provide that feature with code:

What’s next?

In next part of PowerShell fun I am going to refactor code and set some settings like:

  • permissions
  • database connection strings

If you want to check how current version works you can checkout (and later also participate) code from my public repository on GitHub : PowerShell-automation