1

I need to run a script to extract Sitecore item data. As of now if I use the below script:

Get-ChildItem -Path master:/Sitecore/content/Data -Recurse |Show-ListView -Property DisplayName, ItemPath,TemplateName,Language,"Field1","Field2","Field3"

It gives the data in the table and the value of Field1,Field2 and Field3 are also populated for each item.

In this scenario I have to manually find all the fields in item and child item and place them in script like Field1, Field2, Field3 to get their values.

Is there a way I can get the fields from item template and use it in script instead of manually mentioning Field1,Field2,Field3

1
  • With Show-ListView you can omit the Property parameter.to get all properties. Jun 2, 2022 at 5:26

2 Answers 2

0

You can use below script to the item fields

$hashTable = @{} 
Get-ChildItem $rootItem -Recurse | ForEach-Object {
    $currentItem = $_
    $inheritedFields = "";
    Get-ItemField -Item $currentItem -ReturnType Field -Name "*" `
    | Where-Object {$_.ContainsStandardValue} | Select-Object -Property Name,Value `
    | ForEach-Object{ $inheritedFields += ("[" + $_.Name + ": " + $_.Value + "]")}
    if($inheritedFields -ne "")
    {
        $hashTable.Add($currentItem.ContentPath, $inheritedFields)
    }
}

$hashTable | Format-Table -AutoSize

You will get output in table format, I tested it on my Sitecore instance.

0

Yes you can get the item template fields dynamically using this way.

# Get the item children
$items = Get-ChildItem -Path master:/Sitecore/content/Home -Recurse 

# Create a loop on each item 
foreach($item in $items) 
{
    # Get the item template of the item
    $itemTemplate = Get-ItemTemplate $item

    # Get fields 
    $itemTemplateFields = $itemTemplate.OwnFields | Select-Object -ExpandProperty key -Unique 
    $fields =[System.String]::Join(",", ($itemTemplateFields | Where-Object { $standardFields -notcontains $_ } | Sort-Object)) 

    # You will get the field name in a string with a comma-separated value.
    Write-Host $fields
}

Use this in your logic and it will work.

Also to use this variable called $fields, you need to create a dynamic query and then use Invoke-Expression to execute this.

$query = "Get-ChildItem -Path master:/sitecore/content/Home |Show-ListView -Property DisplayName, ItemPath,TemplateName,Language," + $fields

Invoke-Expression $query

Thanks

2
  • When i am trying to add this $fields in the below query : Get-ChildItem -Path master:/Sitecore/content/Data -Recurse |Show-ListView -Property DisplayName, ItemPath,TemplateName,Language,$fields. It Fails. Tried Foreach Object,that also not working.Need the individual fields append to the query GetChildItem
    – Debaparna
    Jun 2, 2022 at 11:49
  • @Debaparna Updated my answer as per your requirement. Jun 2, 2022 at 12:13

Your Answer

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

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