So, sometimes, the content authors and marketers feel that the default value of one of the fields mentioned in template standard values should be updated. The problem here is that the standard value for the field is already being used in hundreds of items derived from this template in the site. Updating the value for field in standard values will update the field value in all these items (if they have not been updated by an author ever). But we want that all the existing items should retain their current value, even if it is derived from standard value item.
The easiest way to execute such updates is via Powershell :)
The below script basically looks for ContainsStandardValue flag in the fields which tells Sitecore that the field value for item is derived from standard values. So our script shall reassign the value to the field programmatically whenever the flag is set to true. Reassigning will set the flag to false. The script generates a report comparing the values before the script is run with the values after the script has run.
$templateIds = '{D1F25FAF-9ECB-4447-A9F6-1F64E3E59B19}' #list of templateIds in which the Standard Value needs to be looked
$global:updatefield = $true
$global:fieldName = "My Field" #name of the field for which standard value needs to be updated
$global:fieldValue = "<value of the field eg. an Id or string>"
$global:languageVersion = "en-us"
$global:contentPath = "/sitecore/content/<site path>"
$reportData = [System.Collections.ArrayList]@()
$reportFields = "ID|FullPath|$($global:fieldName)"
$reportFieldsArray = ($reportFields).Split("|");
$UseHeaderAfter = "UseHeaders Value after updating Standard Value";
Write-Host "---Starting Update Field $($global:fieldName) Script---" -ForegroundColor DarkCyan
Write-Host "Update $($global:fieldName) Field Setting = $($global:updatefield)"
$pages = Get-ChildItem -Path $($global:contentPath) -Language $($global:languageVersion) -Recurse | Where-Object {$templateIds -contains $_.TemplateID}
#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 the field in report before updating its standard value
foreach($page in $pages){
$newReportRecord = Create-Report-Record -item $page -fields $reportFieldsArray;
$reportData.Add($newReportRecord)
}
#Looping through all the items to check if the value of the field is derived from Standard value. If yes, then programatically reassign the same value
foreach($page in $pages){
#check if field is using the standard values or not
$field = Get-ItemField -Item $page -ReturnType Field -Name $($global:fieldName)
$val = $($field.Value)
if ($field.ContainsStandardValue){
Write-Host "$($page.Name) is using standard value - $($field.Name) $val" -ForegroundColor Yellow
if($global:updatefield){
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
#temp update the field programatically to its temp value to force "ContainsStandardValue" to be changed to false. This will ensure these values remain unchanged when Standard Value is updated for this field
$page.Editing.BeginEdit()
$page["$($global:fieldName)"] = "-1"
$page.Editing.EndEdit() | Out-Null
Write-Host "Use Headers field Updated To temp Value: 1 - $($page.Paths.FullPath)" -ForegroundColor DarkGreen
#now set it back to original value again
$page.Editing.BeginEdit()
$page["$($global:fieldName)"] = $val
$page.Editing.EndEdit() | Out-Null
Write-Host "Use Headers Updated To original value: $val" -ForegroundColor Green
}
}
}
else{
Write-Host "$($page.Name) - is NOT using standard value - Skipping Page Item. $($field.Name) $val" -ForegroundColor Magenta
}
}
#Update the standard value for Use Headers field
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
$standardValueItem = Get-Item -Path "master:" -ID "{E36EEB09-F751-400A-A2BD-03F94ACDAFC5}" -Language $($global:languageVersion)
$standardValueItem.Editing.BeginEdit()
$standardValueItem["$($global:fieldName)"] = $($global:fieldValue);
$standardValueItem.Editing.EndEdit() | Out-Null
}
#populating the values in report after updating the standard value
foreach($page in $pages){
foreach($record in $reportData){
if($record.ID -eq $page.ID){
$field = Get-ItemField -Item $page -ReturnType Field -Name $($global:fieldName)
$record.$UseHeaderAfter = $($field.Value)
}
}
}
Write-Host "---Script Completed---" -ForegroundColor DarkCyan
Write-Host "-----------------------------------------"
$reportProperties = @{
Title = "$($global:fieldName) Field Values"
InfoTitle ="$($global:fieldName) Field Values"
InfoDescription = "$($global:fieldName) Field Values"
PageSize = 250
}
### END generate report ###
$reportData | Show-ListView @reportProperties -Property ($reportFieldsArray += @($UseHeaderAfter) )
I hope it helps you :)
Comments
Post a Comment