Updated: Sitecore PowerShell Script to fix HTML in Rich Text Fields


In one of my previous posts, I shared a PowerShell script that is used to fix Subscript or Superscript text in Sitecore Rich Text Fields.


But there is one more requirement in my project which is not fulfilled by that script. The issue with that script is that firstly it didn't work for multiple languages and another issue is that it updated Rich Text Fields based on the field name.


Here is the link to the old post for Sitecore PowerShell Script to fix Subscript or Superscript Text.


https://coderslifeline.blogspot.com/2022/07/sitecore-powershell-script-to-fix.html


Updated Script to fix Subscript or Superscript Text


$items = Get-ChildItem "master:/sitecore/content/Home" -Recurse -Language *

foreach ($item in $items) {
    Write-Host "Item  - " $item.Paths.Path
    foreach ($field in $item.Fields) {
        if ($field.Type -eq "Rich Text") {
            $rawValue = $field.Value
            Write-Host $rawValue
            if ($rawValue -like '®') {
                $output = [System.Web.HttpUtility]::HtmlDecode($rawValue)
                $output = $output -replace "®", '<sup>&reg;</sup>'
                Write-Host $output
                $item.Editing.BeginEdit()
                $item.Fields[$field.Name].Value = $output
                $item.Editing.EndEdit()
                Write-Host "Field - " $field.Name
            }
        }
    }
}


PowerShell scripts are useful as they are development accelerators that increase productivity and also decrease the time required to deliver a solution.


References


Sitecore PowerShell - https://doc.sitecorepowershell.com/


Sitecore PowerShell GitHub - https://github.com/SitecorePowerShell/Console

Chirag Goel

I am a developer, likes to work on different future technologies.

Post a Comment (0)
Previous Post Next Post