src/Solutions/Customization/Forms/Remove-XrmFormControl.ps1
|
<# .SYNOPSIS Remove a PCF custom control from a form field. .DESCRIPTION Remove a Power Apps Component Framework (PCF) custom control binding from a field in a model-driven app form by modifying the FormXML of the systemform record. The FormXML is read with its unpublished changes, which are kept. When no PCF control is left for the field, its binding is removed and the field gets its default control back. .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER FormReference EntityReference of the systemform record to modify. .PARAMETER FieldName Logical name of the field to remove the control from. The control of the form body is used when the field is also in the header. .PARAMETER ControlName Full unique name of the PCF control to remove. Optional. If not specified, removes all custom controls from the field. .PARAMETER Publish Publish the table of the form after the update. (Default: true) .OUTPUTS System.Void. .EXAMPLE $formRef = New-XrmEntityReference -LogicalName "systemform" -Id $formId; Remove-XrmFormControl -FormReference $formRef -FieldName "industrycode" -ControlName "MscrmControls.FieldControls.OptionSet"; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Remove-XrmFormControl.md #> function Remove-XrmFormControl { [CmdletBinding(SupportsShouldProcess)] [OutputType([System.Void])] param ( [Parameter(Mandatory = $false, ValueFromPipeline)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [Microsoft.Xrm.Sdk.EntityReference] $FormReference, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $FieldName, [Parameter(Mandatory = $false)] [string] $ControlName, [Parameter(Mandatory = $false)] [bool] $Publish = $true ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $form = Get-XrmFormXmlInternal -XrmClient $XrmClient -FormId $FormReference.Id -Unpublished; $formXml = $form.FormXml; $controlNode = $formXml.SelectSingleNode("/form/tabs//control[@datafieldname='$FieldName']"); if (-not $controlNode) { $controlNode = $formXml.SelectSingleNode("//control[@datafieldname='$FieldName']"); } if (-not $controlNode) { throw "Field '$FieldName' not found in the form XML."; } $uniqueId = $controlNode.GetAttribute("uniqueid"); $hasControlName = $PSBoundParameters.ContainsKey('ControlName'); $removed = $false; $descriptions = @(); if ($uniqueId) { $descriptions = @($formXml.SelectNodes("//controlDescription")) | Where-Object { $_.GetAttribute("forControl") -ieq $uniqueId }; } foreach ($description in $descriptions) { if ($hasControlName) { $namedControls = @($description.SelectNodes("customControl[@name]")) | Where-Object { $_.GetAttribute("name") -eq $ControlName }; foreach ($namedControl in $namedControls) { $description.RemoveChild($namedControl) | Out-Null; $removed = $true; } if (@($namedControls).Count -eq 0 -or $description.SelectNodes("customControl[@name]").Count -gt 0) { continue; } } else { $removed = $true; } # No PCF control left: remove the binding, the field gets its default control back $defaultControl = $description.SelectSingleNode("customControl[@id]"); if ($defaultControl -and $controlNode.GetAttribute("classid") -ieq "{F9A8A302-114E-466A-B582-6771B2AE0D92}") { $controlNode.SetAttribute("classid", $defaultControl.GetAttribute("id")); } $descriptionsNode = $description.ParentNode; $descriptionsNode.RemoveChild($description) | Out-Null; if (-not $descriptionsNode.HasChildNodes) { $descriptionsNode.ParentNode.RemoveChild($descriptionsNode) | Out-Null; } } if (-not $removed) { Write-Warning "No custom control found for field '$FieldName'."; return; } $updateRecord = New-XrmEntity -LogicalName "systemform" -Id $FormReference.Id -Attributes @{ "formxml" = $formXml.OuterXml; }; $XrmClient | Update-XrmRecord -Record $updateRecord; if ($Publish) { $XrmClient | Publish-XrmComponent -ComponentName "entity" -ComponentId $form.EntityLogicalName | Out-Null; } } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Remove-XrmFormControl -Alias *; |