Using Sitecore PowerShell Extensions to Get/Update Field Values
Bulk review and updating of Sitecore field values within items are a common need, and one that Sitecore PowerShell Extensions (SPE) is uniquely qualified to handle. In this article, we will walk through two sample scripts to get or update field values… with a little extra sauce to help you get started (noting that the scripts are use at your own risk and there is no warranty/accountability to your execution of them).
Specifically, we will:
- Set the content path and field to target
- Target only the desired templates
- Choose whether we will recursively crawl a path or target a single item
- Output to the Sitecore PowerShell Extension (SPE) ISE for viewing (and copy out as needed)
Before jumping in, familiarize yourself with the following SPE pages, as the two examples are meant to get the basics covered while so much more can be accomplished:
Retrieving Items: Retrieving Items | Sitecore PowerShell Extensions
Editing Items: Editing Items | Sitecore PowerShell Extensions
Get Field Values Sample Script and Explanation
The following script gets field values using a starting path and templates to target. This is critical in that you may not want to get field values across all templates and always start at the root path. You can adjust these variables to meet your targeting needs.
Of note, templates are designated as an array with a “TemplateType”. This is useful in case you want to target modules as well, since you could update the logic of the sample script to a TemplateType = ‘module’ and later refine your Where-Object to “TemplateType -eq ‘module’ as an example. The goal of this targeting is to provide a flexible manner to retrieve the Sitecore TemplateId as that is the most direct way to ensure you are getting the correct template(s).
Once templates are defined, you can choose to either recursively go through a path searching for all templates within the content path or change the commenting to a single item (note that you either choose “Nested/Multi Page Target” or “Single Page Target”, not both in my example).
With these now defined, a loop executes over the items in scope and runs an operation to get the field values and outputs them on screen.
# FIND TARGETED FIELD VALUES
# Description: Finds value of the following fields across a nested set of pages:
# - My Field
#
# Author: Kelly Rusk
# Content Path to Find Data
$contentPath = "master:/sitecore/content/Home" # Adjust the path as needed
$sitecoreFieldName = "My Field" # Sitecore field to target
# Templates to Target
$templates = @(
[PSCustomObject]@{ Name = 'Sample Page'; TemplateId = '{0719EEFC-6734-4207-9E31-F228D27C979A}'; TemplateType = 'page' }
[PSCustomObject]@{ Name = 'Example Page'; TemplateId = '{D079D06A-3A58-4065-A762-65F4897087C9}'; TemplateType = 'page' }
)
# Get Items by Page Template ID
$pageTemplateIds = $templates | Where-Object { $_.TemplateType -eq 'page' } | Select-Object -ExpandProperty TemplateId
# Nested/Multi Page Target - Uncomment to Use. IMPORTANT: Must either use Nested/Multi or Single, not both uncommented
$items = Get-ChildItem -Path $contentPath -Recurse | Where-Object { $pageTemplateIds -contains $_.TemplateId }
# Single Page Target - Uncomment to Use. IMPORTANT: Must either use Nested/Multi or Single, not both uncommented
# $items = Get-Item -Path $contentPath | Where-Object { $pageTemplateIds -contains $_.TemplateId }
foreach ($i in $items) {
$fieldName = $sitecoreFieldName
$fieldValue = $i[$fieldName]
if ($fieldValue) {
# Write Output to Screen
Write-Host "Name: $($i.Name) | ID: $($i.ID) | PATH: $($i.FullPath)" -ForegroundColor Cyan
Write-Host "$($fieldName): $fieldValue"
Write-Host "`n"
}
}
Update Field Values Sample Script and Explanation
Similar to the “Get Field Values” sample script above, the “Update Field Values” script also uses content path with recursion or non-recursion options and template targeting. Where it differs is in one key element as follows: $i.$fieldName = $newFieldValue
This is where it sets the field to a new value. In many cases you will not want to set the same value for a field across multiple items and here is where you can add your individual business logic to bulk update field values.
# Updates TARGETED FIELD VALUES
# Description: Updates value of the following fields across a nested set of pages:
# - My Field
#
# Author: Kelly Rusk
# Content Path to Find Data
$contentPath = "master:/sitecore/content/Home" # Adjust the path as needed
$sitecoreFieldName = "My Field" # Sitecore field to target
# Templates to Target
$templates = @(
[PSCustomObject]@{ Name = 'Sample Page'; TemplateId = '{0719EEFC-6734-4207-9E31-F228D27C979A}'; TemplateType = 'page' }
[PSCustomObject]@{ Name = 'Example Page'; TemplateId = '{D079D06A-3A58-4065-A762-65F4897087C9}'; TemplateType = 'page' }
)
# Get Items by Page Template ID
$pageTemplateIds = $templates | Where-Object { $_.TemplateType -eq 'page' } | Select-Object -ExpandProperty TemplateId
# Nested/Multi Page Target - Uncomment to Use. IMPORTANT: Must either use Nested/Multi or Single, not both uncommented
$items = Get-ChildItem -Path $contentPath -Recurse | Where-Object { $pageTemplateIds -contains $_.TemplateId }
#Single Page Target - Uncomment to Use. IMPORTANT: Must either use Nested/Multi or Single, not both uncommented
# $items = Get-Item -Path $contentPath | Where-Object { $pageTemplateIds -contains $_.TemplateId }
foreach ($i in $items) {
$fieldName = $sitecoreFieldName
$fieldValue = $i[$fieldName]
if ($fieldValue) {
# Set field value
$newFieldValue = "New Value"
$i.$fieldName = $newFieldValue
# Write Output to Screen
Write-Host "Name: $($i.Name) | ID: $($i.ID) | PATH: $($i.FullPath)" -ForegroundColor Cyan
Write-Host "$($fieldName): $newFieldValue"
Write-Host "`n"
}
}
An example of custom business logic would be if you wanted to remove trailing slashes from fields that have an URL where you could use the following Regex based logic instead of trying to loop over an array of item matching to set fields in bulk:
if ($fieldValue) {
# Remove Trailing Slash but keep URL intact
$removeTrailing = $fieldValue -replace '(url=["''])(.*?)\s*/+\s*(?=[?#"''])', '$1$2'
# Set field value
$i.$fieldName = $removeTrailing
# Write Output to Screen for Optional Copy to Word
Write-Host "Name: $($i.Name) | ID: $($i.ID) | PATH: $($i.FullPath)" -ForegroundColor Cyan
Write-Host "$($fieldName): $removeTrailing"
Write-Host "`n"
}
Conclusion
In essence, there are so many options when it comes to field-based viewing and manipulation using Sitecore PowerShell Extensions (SPE). You can use the sample scripts above to understand the core concepts of targeting and getting/updating fields before expanding into more complex business requirements. Of note, the scripts are not under any warranty/accountability and solely use at your own risk.
