In this post, I am going to share a script which can help you find all the pages which contain a keyword in its content. This requirement came from the business team who wanted some insights about pages which use keywords like finance, commerce etc.
One can easily get this information from search functionality on Sitecore site or client. Or one can always create a powershell script and generate a report that can be shared with business users (yes, they love reports).
We had a few assumptions -
- We looked for keyword only in Rich text fields. You can modify script to include more field types.
- We needed the page links rather than Sitecore content path as the business users were not familiar with Sitecore.
- Mostly, the keyword was present in content items placed within _content folder under the page. For such items, we preferred to resolve the URL to the page ancestor to the content item. For this, we checked if the item path contains _content. If yes, then we limited the URL to page item only by removing the part containing the path to the content item in the url.
- E.g. For item, /sitecore/content/SiteA/AboutUs/_content/heroBannerItem. Page Url - https://<domain>/AboutUs
So, here is the script -
$reportData = [System.Collections.ArrayList]@()
$reportFields = "ID|Name"
$reportFieldsArray = ($reportFields).Split("|");
$url="Url";
Write-Host "---Starting the Script---" -ForegroundColor DarkCyan
$sites = Get-Item -Path "/sitecore/content/SiteA"
$items = Get-ChildItem -Path $sites.ProviderPath -Recurse -Language * | Where-Object { $_.Fields | Where-Object {$_.Type -eq "Rich Text"} | Where-Object {$_.Value -like "*commercial*"}}
function Get-ItemUrl($itemToProcess){
[Sitecore.Context]::SetActiveSite("website")
$urlop = New-Object ([Sitecore.Links.UrlOptions]::DefaultOptions)
$urlop.AddAspxExtension = $false
$urlop.AlwaysIncludeServerUrl = $true
$linkUrl = [Sitecore.Links.LinkManager]::GetItemUrl($itemToProcess,$urlop)
$linkUrl
}
#Reusable function to create record for the report
function Create-Report-Record {
param(
$item,
$fields
)
[System.Collections.Hashtable]$newReportRecord = @{}
$fields | ForEach-Object {
$currentField = $_
$newReportRecord.$currentField = $item.$currentField;
}
return $newReportRecord;
}
#capturing all of the values of Use Headers field in report before updating its standard value
foreach($item in $items){
$pageUrl=Get-ItemUrl($item);
$separator = [string[]]@("_content")
if ($pageUrl -match $separator) {
$tempArray = $pageUrl.Split($separator,[System.StringSplitOptions]::RemoveEmptyEntries);
$pageUrl=$tempArray[0];
}
$newReportRecord = Create-Report-Record -item $item -fields $reportFieldsArray;
$newReportRecord.$url=$pageUrl;
$reportData.Add($newReportRecord)
}
Write-Host "---Script Completed---" -ForegroundColor DarkCyan
Write-Host "-----------------------------------------"
$reportProperties = @{
Title = "List of items containing - Commercial"
InfoTitle ="List of items containing - Commercial"
InfoDescription = "List of items containing - Commercial"
PageSize = 250
}
### END generate report ###
$reportData | Show-ListView @reportProperties -Property ($reportFieldsArray+=@($url))
Hope this helps you! Please like and subscribe the blog :)
Comments
Post a Comment