src/Query/New-XrmQueryExpression.ps1
|
<# .SYNOPSIS Return QueryExpression object instance. .DESCRIPTION Initialize new query expression object, with NoLock set. .PARAMETER LogicalName Gets or sets the entity name to query. .PARAMETER Columns Gets or sets the columns to return in the query results. "*" returns all columns; without this parameter, only the primary key is returned. .PARAMETER TopCount Gets or sets the number of rows to be returned. Without this parameter, no limit is set. .OUTPUTS Microsoft.Xrm.Sdk.Query.QueryExpression. The query, for pipeline chaining. .EXAMPLE $xrmClient = New-XrmClient -ConnectionString $connectionString; $query = New-XrmQueryExpression -LogicalName "account" -Columns "name", "accountnumber" -TopCount 50; $query = $query | Add-XrmQueryCondition -Field "statecode" -Condition Equal -Values 0; $accounts = $xrmClient | Get-XrmMultipleRecords -Query $query; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/New-XrmQueryExpression.md #> function New-XrmQueryExpression { [CmdletBinding()] [OutputType("Microsoft.Xrm.Sdk.Query.QueryExpression")] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $LogicalName, [Parameter(Mandatory = $false)] [String[]] $Columns, [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [int] $TopCount = 1000 ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $query = New-Object -TypeName "Microsoft.Xrm.Sdk.Query.QueryExpression" -ArgumentList $LogicalName; if ($PSBoundParameters.ContainsKey('Columns')) { if ($Columns.Contains("*")) { $query.ColumnSet.AllColumns = $true; } else { $Columns | ForEach-Object { $query.ColumnSet.AddColumn($_); } } } $query.NoLock = $true; if ($PSBoundParameters.ContainsKey('TopCount')) { $query.TopCount = $TopCount; } $query; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function New-XrmQueryExpression -Alias *; Register-ArgumentCompleter -CommandName New-XrmQueryExpression -ParameterName "LogicalName" -ScriptBlock { param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters) $validLogicalNames = Get-XrmEntitiesLogicalName; return $validLogicalNames | Where-Object { $_ -like "$wordToComplete*" }; } Register-ArgumentCompleter -CommandName New-XrmQueryExpression -ParameterName "Columns" -ScriptBlock { param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters) if (-not ($fakeBoundParameters.ContainsKey("LogicalName"))) { return @(); } $validAttributeNames = Get-XrmAttributesLogicalName -EntityLogicalName $fakeBoundParameters.LogicalName; return $validAttributeNames | Where-Object { $_ -like "$wordToComplete*" }; } |