src/Solutions/Customization/Views/Add-XrmView.ps1
|
<# .SYNOPSIS Create a new view in Microsoft Dataverse. .DESCRIPTION Create a new savedquery record (system view). .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER EntityLogicalName Table / Entity logical name the view belongs to. .PARAMETER Name View display name. .PARAMETER Labels Hashtable of language code to display name. Alternative to -Name. The stored 'name' is resolved from -LanguageCode (fallback: lowest language code), and every provided language is persisted as a real translation via SetLocLabels so each user sees the label in their own language. Example: @{ 1033 = "Active accounts"; 1036 = "Comptes actifs" } .PARAMETER LanguageCode Language code used to pick the stored 'name' from -Labels. Default: 1033. .PARAMETER FetchXml FetchXml query for the view. .PARAMETER LayoutXml Layout XML defining column widths and order. .PARAMETER QueryType View query type. Default: 0 (public view). .PARAMETER Description View description. .PARAMETER SolutionUniqueName Unmanaged solution unique name. When provided, the created view is automatically added to this solution. .OUTPUTS Microsoft.Xrm.Sdk.EntityReference. Reference to the created savedquery record. .EXAMPLE $ref = Add-XrmView -EntityLogicalName "account" -Name "Active Accounts" -FetchXml $fetchXml -LayoutXml $layoutXml; $ref = Add-XrmView -EntityLogicalName "account" -Name "Active Accounts" -FetchXml $fetchXml -LayoutXml $layoutXml -SolutionUniqueName "MySolution"; .EXAMPLE $ref = Add-XrmView -EntityLogicalName "account" -Labels @{ 1033 = "Active accounts"; 1036 = "Comptes actifs" } -LanguageCode 1036 -FetchXml $fetchXml -LayoutXml $layoutXml; #> function Add-XrmView { [CmdletBinding(DefaultParameterSetName = "ByName")] [OutputType([Microsoft.Xrm.Sdk.EntityReference])] param ( [Parameter(Mandatory = $false, ValueFromPipeline)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $EntityLogicalName, [Parameter(Mandatory = $true, ParameterSetName = "ByName")] [ValidateNotNullOrEmpty()] [string] $Name, [Parameter(Mandatory = $true, ParameterSetName = "ByLabels")] [ValidateNotNullOrEmpty()] [Hashtable] $Labels, [Parameter(Mandatory = $false)] [int] $LanguageCode = 1033, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $FetchXml, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $LayoutXml, [Parameter(Mandatory = $false)] [int] $QueryType = 0, [Parameter(Mandatory = $false)] [string] $Description, [Parameter(Mandatory = $false)] [string] $SolutionUniqueName ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { if ($PSCmdlet.ParameterSetName -eq "ByLabels") { $Name = Get-XrmLabelText -Labels $Labels -LanguageCode $LanguageCode; } $record = New-XrmEntity -LogicalName "savedquery" -Attributes @{ "returnedtypecode" = $EntityLogicalName; "name" = $Name; "fetchxml" = $FetchXml; "layoutxml" = $LayoutXml; "querytype" = $QueryType; }; if ($PSBoundParameters.ContainsKey('Description')) { $record.Attributes["description"] = $Description; } $id = Add-XrmRecord -XrmClient $XrmClient -Record $record; $viewReference = New-XrmEntityReference -LogicalName "savedquery" -Id $id; # Persist the multilingual name as real translations (SetLocLabels) so each language sees its own label. if ($PSCmdlet.ParameterSetName -eq "ByLabels") { Set-XrmLocalizedLabel -XrmClient $XrmClient -EntityMoniker $viewReference -AttributeName "name" -Labels $Labels | Out-Null; } if ($PSBoundParameters.ContainsKey('SolutionUniqueName')) { Add-XrmSolutionComponent -XrmClient $XrmClient -SolutionUniqueName $SolutionUniqueName -ComponentId $id -ComponentType 26 -DoNotIncludeSubcomponents $false | Out-Null; } $viewReference; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Add-XrmView -Alias *; Register-ArgumentCompleter -CommandName Add-XrmView -ParameterName "EntityLogicalName" -ScriptBlock { param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters) $validLogicalNames = Get-XrmEntitiesLogicalName; return $validLogicalNames | Where-Object { $_ -like "$wordToComplete*" }; } |