0

Rather than create a static HashTable that is composed of the data from my list, I am looking to make it dynamic, but ran into some difficulty in constructing it.

For example:

Given that $items is set to Get-PnPListItem

foreach($item in $items){
    $HashTable += New-Object PSObject -Property @{
        'Title' = $item.FieldValues.Title;
        'Date' = $item.FieldValues.Date;
        'Column 3' = $item.FieldValues.Column_x0020_3;
        'Column 4' = $item.FieldValues.Column_x0020_4;
        'Assigned To' = $item.FieldValues.Assigned_x0020_To.LookupValue;
    }
}

That example above works, but again, is explicit. My thought was to construct the HashTable from the All Items view.

$view = Get-PnPView -List $listName -Identity "All Items";
$columns = $view.ViewFields;

Since a foreach() cannot be run inside of the HashTable, I have tried piping Add and Add-Member from a foreach of $column in $columns, but without resolve.

Within the loop, I would also need to check each GetType() to determine if I need to append a .LookupValue, etc.

While I can get by with what I have and use explicit scripts, it should be self-evident how valuable a dynamically constructed script would be for all.

Any ideas?

1 Answer 1

1

The following PowerShell for your reference.

$listName="TestList"
$items=Get-PnPListItem -List $listName
$view = Get-PnPView -List $listName -Identity "All Items"
$columns = $view.ViewFields
$HashTable=@()
foreach($item in $items){
    $obj=New-Object -TypeName PSObject
    $HashTable += $obj  
    foreach($column in $columns){
        if($column -eq "LinkTitle"){    
            $obj | Add-Member -MemberType NoteProperty -Name Title -Value $item.FieldValues.Title
        }else{
            if($item.FieldValues[$column].toString() -eq "Microsoft.SharePoint.Client.FieldLookupValue"){
                $obj | Add-Member -MemberType NoteProperty -Name $column -Value $item.FieldValues[$column].LookupValue
            }else{
                $obj | Add-Member -MemberType NoteProperty -Name $column -Value $item.FieldValues[$column]
            }

        }
    }           
}
0

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.