Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Last active April 18, 2020 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swimburger/4b1a7583be2ba286ebf404e083287893 to your computer and use it in GitHub Desktop.
Save Swimburger/4b1a7583be2ba286ebf404e083287893 to your computer and use it in GitHub Desktop.
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