src/Query/Get-XrmRecordsFromView.ps1
|
<# .SYNOPSIS Retrieve records from a view. .DESCRIPTION Get records according to given view name. .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER EntityLogicalName Table logical name of the view. .PARAMETER ViewName System view (saved query) name. Use auto completion to select proper one. An error is raised when no view or several views of the table have this name. .OUTPUTS PSCustomObject[]. Rows returned by the view query (see Get-XrmMultipleRecords). .EXAMPLE $accounts = Get-XrmRecordsFromView -XrmClient $xrmClient -EntityLogicalName "account" -ViewName "Active Accounts"; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Get-XrmRecordsFromView.md #> function Get-XrmRecordsFromView { [CmdletBinding()] [OutputType([PSCustomObject[]])] param ( [Parameter(Mandatory = $false, ValueFromPipeline)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $EntityLogicalName, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $ViewName ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $selectedViews = @($XrmClient | Get-XrmViews -EntityLogicalName $EntityLogicalName -Columns "name", "layoutxml", "fetchxml" | Where-Object -Property "name" -EQ $ViewName); # Two views of the same name gave an array of FetchXml, hence an invalid query (B7) if ($selectedViews.Count -eq 0) { throw "View '$ViewName' not found on table '$EntityLogicalName'."; } if ($selectedViews.Count -gt 1) { throw "Several views of table '$EntityLogicalName' are named '$ViewName' ($(($selectedViews | ForEach-Object { $_.Id }) -join ', ')). Read the one you want with Get-XrmViews, then query it with Get-XrmQueryFromFetch and Get-XrmMultipleRecords."; } $fetchQuery = $XrmClient | Get-XrmQueryFromFetch -FetchXml $selectedViews[0].fetchxml; $records = $XrmClient | Get-XrmMultipleRecords -Query $fetchQuery; $records; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Get-XrmRecordsFromView -Alias *; Register-ArgumentCompleter -CommandName Get-XrmRecordsFromView -ParameterName "EntityLogicalName" -ScriptBlock { param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters) $validLogicalNames = Get-XrmEntitiesLogicalName; return $validLogicalNames | Where-Object { $_ -like "$wordToComplete*" }; } Register-ArgumentCompleter -CommandName Get-XrmRecordsFromView -ParameterName "ViewName" -ScriptBlock { param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters) $viewsNames = @(); if (-not ($fakeBoundParameters.ContainsKey("EntityLogicalName"))) { return $viewsNames; } $views = Get-XrmViews -EntityLogicalName $fakeBoundParameters.EntityLogicalName -Columns "name"; $views | ForEach-Object { $viewsNames += $_.name }; return $viewsNames | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object; } |