Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all option values and labels for Dynamics CRM Entity OptionSet Attribute
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
You can’t perform that action at this time.