Serge van den Oever's weblog

Sitecore 9.3 SXA CLI - get item fields

Mon Feb 03 2020 • ☕️ 2 min read • you like my writing? Buy me a coffee

In my blogpost Sitecore 9.3 - create a custom theme for SXA using SXA CLI I describe how to get started with SXA CLI. When configured as described in the blog post, and with the PowerShell remoting enabled, it is easy to build utility scripts for things like getting the fields of a Sitecore item. This is very useful in writing rendering variants using the new Scriban templating language. Add the following script PowerShell script to the root of your theme folder with the name get-itemfields.ps1, and with the command:

.\get-itemfields.ps1 -itemPath /sitecore/home/content

you will get an overview of all fields defined on the item:

Name                                DisplayName                        SectionDisplayName Description                                       
----                                -----------                        ------------------ -----------                                       
Text                                Text                               Data               The text is the main content of the document.     
Title                               Title                              Data               The title is displayed at the top of the document.
__Enable item fallback              Enable Item Fallback               Advanced                                                             
__Enforce version presence          Enforce Version Presence           Advanced                                                             
__Source Item                       __Source Item                      Advanced                                                             
__Source                            Source                             Advanced                                                             
__Standard values                   Standard values                    Advanced
:                                                             

The PowerShell script get-itemfields.ps1:

# Display fields of Sitecore item
# https://www.sergevandenoever.nl
param(
    [string]$itemPath = "/sitecore/content/Home"
)

Set-Location -Path $PSScriptRoot

$configParseResult = select-string -path gulp\config.js -pattern "user: { login: '(.*)', password: '(.*)' }"
if ($configParseResult.Matches.Groups.Length -ne 3) {
    Write-Error "Expected file gulp\config.js to contain a line in the format: user: { login: 'sitecore\\admin', password: 'b' }"
    Exit
}
$username = $configParseResult.Matches.Groups[1].Value.Replace('\\', '\')
$password = $configParseResult.Matches.Groups[2].Value

if ($username -eq '' -or $password -eq '') {
    Write-Error "Expected file gulp\config.js to contain a line in the format: user: { login: 'sitecore\\admin', password: 'b' }, login or password is empty."
    Exit
}

$configParseResult = select-string -path gulp\config.js -pattern "server: '(.*)'"
if ($configParseResult.Matches.Groups.Length -ne 2) {
    Write-Error "Expected file gulp\config.js to contain a line in the format: server: 'https://myserver.dev.local/'"
    Exit
}
$server = $configParseResult.Matches.Groups[1].Value
if ($server -eq '') {
    Write-Error "Expected file gulp\config.js to contain a line in the format: server: 'https://myserver.dev.local/', server is empty."
    Exit
}

$serverConfigResult = Get-Content -Path gulp\serverConfig.json | ConvertFrom-Json
$projectPath = $serverConfigResult.serverOptions.projectPath

Write-Output "Username: $username"
Write-Output "Password: $password"
Write-Output "ConnectionUri: $server"
Write-Output "Get item fields of '$itemPath'..."
Import-Module -Name SPE 
$session = New-ScriptSession -Username $username -Password $password -ConnectionUri $server
Invoke-RemoteScript -Session $session -ScriptBlock { 
    Get-Item $Using:itemPath | Get-ItemField -IncludeStandardFields -ReturnType Field -Name "*" | Format-Table Name, DisplayName, SectionDisplayName, Description -auto
}
Stop-ScriptSession -Session $session

Discuss on TwitterEdit on GitHub

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. You are free to share and adapt this work for non-commercial purposes, provided you give appropriate credit, provide a link to the license, and indicate if changes were made. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/.

Serge van den Oever's weblog

Serge van den Oever

Personal blog by Serge van den Oever - als je maar lol hebt...
X: @svdoever
LinkedIn: Serge van den Oever - articles on LinkedIn
GitHub: svdoever

Technology Consultant @ Macaw
2021-2024 Sitecore Technology MVP