In our Sitecore application, we had requirement to replace/remove style classes or tokens in entire Sitecore content tree. The site was live and had lots of content. So we needed to go through each and every field in Sitecore content tree and look for those keywords.
Sitecore power shell is the best way to do such bulk search/replacements.
Below script can be run in a Sitecore instance to lookup for specific keywords in Sitecore fields. The script generates a report at the end which shows the list of all occurrences.
$startPath = "master:/sitecore/content/<site path>"
Write-Host "Search started $(Get-Date -format 'u')"
$reportFields = "ID|Language|TemplateName|FieldName|FieldType|Path|FieldValue"
$reportFieldsArray = ($reportFields).Split("|");
$searchText = "<search text>"
$list = [System.Collections.ArrayList]@()
$itemsToProcess = Get-ChildItem $startPath -Language * -Recurse
if($itemsToProcess -ne $null) {
$itemsToProcess | ForEach-Object {
$match = 0;
foreach($field in $_.Fields) {
if($field.Type -eq "Rich Text" -or $field.Type -eq "General Link" -or $field.Type -eq "Single-Line Text" -or $field.Type -eq "Multiline Text" -or $field.Type -eq "Image") {
if($field -match $searchText) {
$info = [PSCustomObject]@{
"ID"=$_.ID
"Language"=$_.Language
"TemplateName"=$_.TemplateName
"FieldName"=$field.Name
"FieldType"=$field.Type
"Path"=$_.FullPath
"FieldValue"=$field
}
[void]$list.Add($info)
}
}
}
}
}
Write-Host "Search ended $(Get-Date -format 'u')"
Write-Host "Items found: $($list.Count)"
##$list | Format-Table
$reportProperties = @{
Title = "$searchText occurrences in content tree"
InfoTitle ="$searchText occurrences"
InfoDescription = "$searchText occurrences"
PageSize = 250
}
$list | Show-ListView @reportProperties -Property ($reportFieldsArray)
I hope you find it uselful! Please share and subscribe to the blog. Thankyou :)
Comments
Post a Comment