Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get all option values and labels for Dynamics CRM Entity OptionSet Attribute
# Import the Dynamics XRM PowerShell module
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment