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
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments