1

Question from .Sitecore PowerShell stater.

I am using following script

$eventsPath = "/sitecore/content/Global Content/Events"
$templateId = "{EBE88C21-4035-4266-9647-B4C88F1BC000}"

# Function to get data and print on the screen
function Print-EventData($item) {
    $itemId = $item.ID
    $title = $item.Name
    $eventTitle = $item.DisplayName
    $created = $item.Statistics.Created
    $updated = $item.Statistics.Updated
    
    $eventDetails = $item.'Event Details'
    $eventTitleFromDetails = $eventDetails.'Event Title'
    $imagePath = $item.'Image'
    

    Write-Host "Item ID:$itemId , title = $title ,Event Title: $eventTitleFromDetails, Created: $created, Updated: $updated, Image Path: $imagePath"
}

# Get the root item
$rootItem = Get-Item -Path $eventsPath

# Process the root item and its children
foreach ($item in Get-ChildItem -Path $rootItem.FullPath -Recurse) {
    if ($item.TemplateID -eq $templateId) {
        Print-EventData -item $item
    }
}

I am not able to get the image url($imagePath) path and, Event title($eventTitleFromDetails).

In my solution all the events items are are created from Event page template, and I see following fields Also in the event item I can see this Images field enter image description here And in template ineritance of event template page can see this being inherited enter image description here

  1. Any suggestions or updation to above command to get this fields?
  2. Also how to get the each field and their value for all the items in that path and created from that Template ID, overall all the field and its value of item created from that template Id and on path ?

As above script is not able get some fields which are there in template , and this $item variable contains only following($item is Sitecore.Data.Items.Item) enter image description here

4 Answers 4

0

Use the following script to access Event Title, Event Details, ID of media item stored in Image field and its full path:

echo ('Title: ' + $item["Event Title"])
echo ('Details: ' + $item["Event Details"])

[Sitecore.Data.Fields.ImageField] $imageField = $item.Fields["Image"]
echo ('Image id: ' + $imageField.MediaID.ToString())

$imageItem = Get-Item -Path master: -ID $imageField.MediaID
echo ('Image path: ' + $imageItem.Paths.FullPath)
0

Marek has already answered your 1st question. For your 2nd question, here is my suggestion -

function GetAllCustomFields {
param(
    [object]$TemplateItem
)

$allfields = ([Sitecore.Data.Items.TemplateItem]$TemplateItem).Fields
# filter out standard fields
$customFields = $allfields | Where-Object { $_.Key -notlike '__**' }

return $customFields
    
}

$eventsPath = "/sitecore/content/Global Content/Events"
$templateId = "{EBE88C21-4035-4266-9647-B4C88F1BC000}"

$rootItem = Get-Item -Path $eventsPath
$templateItem = Get-Item -Path "master:" -ID $templateId 

$allFields = GetAllCustomFields($templateItem)

foreach ($item in Get-ChildItem -Path $rootItem.FullPath -Recurse) {
write-host("--- Item Name - $($item.Name)")
    foreach($field in $allFields){
        write-host "$($field.Name) : $($item[$field.Key])" 
    }
    
}

Above script should give you a list of all custom fields (all own fields + inherited fields except standard fields) with raw values. You can custom logic to display field values differently depending on field type (For example, as Marek did for Media field).

This is how it should displays results -

enter image description here

Hope this helps !

0

You can use below snippet to get all the fields on the item, contained inside the template excluding standard fields.

function GetItemFields {
param([string]$id,[string]$templateid)

$childItems = Get-ChildItem -Path "master:" -ID $id -Recurse
foreach ($item in $childItems) {
   if($item.TemplateID -eq $templateid ){
       $fields = $item | Get-ItemField -ReturnType Field

        $arrayOfFields = @()
        Write-Host "Item Path : $($item.Paths.Fullpath)"
        foreach($field in $fields){
           $obj = New-Object -TypeName PSObject
           $obj | Add-Member -Name 'Name' -MemberType Noteproperty -Value  $field.Name
           $obj | Add-Member -Name 'Value' -MemberType Noteproperty -Value $item.Fields[$field.Name].Value
           $obj | Add-Member -Name 'Type' -MemberType Noteproperty -Value  $field.Type
           $arrayOfFields+=$obj
        }

        $arrayOfFields | Format-Table
     }
  }
}

GetItemFields "{{Item ID of the Parent Folder}}" "{{Template ID}}"

This will return the output in the following format:

enter image description here

You can find more options on Get-ItemField on the Sitecore Appendix below: https://doc.sitecorepowershell.com/appendix/common/get-itemfield

Moreover, in case you need to read image fields through Powershell, you can refer below snippet.

function ReadImageSrc {
param([string]$id, [string]$siteName)

$item = Get-Item -Path "master:" -ID $id
[Sitecore.Data.Fields.ImageField]$imageField = $itemContentBlock.Fields["Image"]

if($imageField){
   $siteContext = [Sitecore.Configuration.Factory]::GetSite($siteName);
   New-UsingBlock(New-Object Sitecore.Sites.SiteContextSwitcher $siteContext) {
     $src = [Sitecore.Resources.Media.MediaManager]::GetMediaUrl($imageField.MediaItem)
     Write-Host $src
   }
  }
 }

ReadImageSrc "{Item Id}" "{{Site Name}}"

Hope this helps!!!

0

If you want to get the value of the Image field then you can use this approach.

[Sitecore.Data.Fields.ImageField] $field = $item.Fields["Image"]
$imageItem = $newitem = Get-Item -Path master: -ID $field.mediaID
Write-Host $imageItem.Paths.FullPath

You can get the list of fields of an item using Get-ItemField. Refer to this. And using a loop on the fields you can get its value by casting it to the appropriate field type. That I showed you the above for the image field.

Check the field type like this in your loop.

$_.Type -eq 'Image'

The complex Sitecore fields store the raw value, which contains the ID of the referenced item, not its value. So according to your field type, you can cast that and get its value.

Here is another example to get the multilist filed like below.

$multilistField = [Sitecore.Data.Fields.MultilistField]$item.Fields["FieldName"]

So create your script in this way to get the values of these complex types.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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