1

I'm trying to write a simple Powershell script that finds all General Link fields and outputs the value of their Source field.

$fields = Get-ChildItem -Path "master://sitecore/templates/user defined" -Recurse | Where-Object { $_.TemplateName -eq "Template field" -and $_.Type -eq "General Link" }

foreach ($field in $fields) {
    Write-Host "Name: $($field.Name)"
    Write-Host "Source: $($field.Source)"
    Write-Host
}

Whenever I run it, however, the value from the Source field is empty for all returned items.

Returned list of fields and empty sources

I've confirmed that it is populated in at least some of the cases, but it never shows up in the report. I've tried several variations to get the value, too:

$fields | Show-ListView -property `
    @{ Name="Path"; Expression={$_.Paths.Path} },
    @{ Name="Source"; Expression={$_.Source} },
    @{ Name="Source With Quotes"; Expression={$_."Source"} }

Versions

  • Sitecore version: 8.1 Update 3
  • Sitecore PowerShell Extensions version: 5.0.0.42513
5

There is no Source property on Sitecore.Data.Items.Item class.

You need to get Source field value e.g. like this:

foreach ($field in $fields) {
    Write-Host "Name: $($field.Name)"
    Write-Host "Source: $($field._.Source.Value)"
    Write-Host
}
  • 2
    This example is making use of a custom object SPE adds to the item. You can use _ or PSFields to get typed properties. – Michael West Feb 15 at 16:25
2

The issue is that the Source field is of type Template Field Source, which seems to not automatically emit a value if you simply access the field directly on the item ($field.Source).

Instead, you must get the field and get the value from that field:

$field.Fields['Source'].Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.