Last active
April 18, 2020 00:27
-
-
Save Swimburger/4b1a7583be2ba286ebf404e083287893 to your computer and use it in GitHub Desktop.
Get all option values and labels for Dynamics CRM Entity OptionSet Attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$true)] | |
[String] | |
$ConnectionString, | |
[Parameter(Mandatory=$true)] | |
[String] | |
$EntityLogicalName, | |
[Parameter(Mandatory=$true)] | |
[String] | |
$OptionSetAttributeName | |
) | |
# Import the Dynamics XRM PowerShell module | |
# https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell/3.3.0.887 | |
Import-Module Microsoft.Xrm.Tooling.CrmConnector.PowerShell; | |
# Get a CrmServiceClient to communicate with Dynamics CRM | |
$CrmClient = Get-CrmConnection -ConnectionString $ConnectionString; | |
# Create a RetrieveAttributeRequest to fetch Attribute metadata | |
$AttributeRequest = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest]::new(); | |
$AttributeRequest.EntityLogicalName = $EntityLogicalName; | |
$AttributeRequest.LogicalName = $OptionSetAttributeName; | |
$AttributeRequest.RetrieveAsIfPublished = $True; | |
$AttributeResponse = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse]$CrmClient.Execute($AttributeRequest); | |
# Get the Value/Label pairs and print them to the console | |
$AttributeResponse.AttributeMetadata.OptionSet.Options ` | |
| Select-Object -Property ` | |
@{Name = "Value"; Expression={$_.Value}},` | |
@{Name = "Label"; Expression={$_.Label.UserLocalizedLabel.Label}}; | |
# Close the connection to CRM | |
$CrmClient.Dispose(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment