web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get option set label from fetchXML aggregate query.

(0) ShareShare
ReportReport
Posted on by

Hi, 

I'm trying to get the option set label from the fetchXML query with no luck. 

I've read many posts and tried different ways to retrieve the option set values that are aggregated (group by) in the fetchXML. 

Below is my query. 

var strQuery = $@"
                <fetch aggregate='true'>
                  <entity name='custasset'>
                    <attribute name='name' alias='totalCount' aggregate='count' />
                    <attribute name='field1' alias='sv' groupby='true' />
                    <attribute name='field2' alias='shv' groupby='true' />
                    <attribute name='field3' alias='rg' groupby='true' />
                    <filter type='and'>
                      <condition attribute='field1' operator='not-null' />
                      <condition attribute='field2' operator='not-null' />
                      <condition attribute='field3' operator='not-null' />                    
                    </filter>
                  </entity>
                </fetch>";

Here is the casting that I tried to get the label. It doesn't even give the option set value. 

1. String optValue = (String)((AliasedValue)c["shv"]).Value; 
2. String optValue = c.GetAttributeValue<String>("sv");

It would be a great help if anyone suggest/guide on how to do this.
Thanks,
Sandip

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    EntityCollection results = (EntityCollection)service.RetrieveMultiple(new FetchExpression(strQuery));            

               foreach (var c in results.Entities) {

                   System.Console.WriteLine(c.Attributes["shv"]);

    //Here I want to get the option set label or value if label is not possible.

               }

  • Suggested answer
    Abby Kong Profile Picture
    6 on at

    Hi Sandip,

    Include below function in your code and call with GetOptionSetValueLabel("custasset", "field2", ((OptionSetValue)((AliasedValue)c["shv"]).Value).Value, service):

        public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
        {
    
            var attReq = new RetrieveAttributeRequest();
            attReq.EntityLogicalName = entityName;
            attReq.LogicalName = fieldName;
            attReq.RetrieveAsIfPublished = true;
    
            var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
            var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;
    
            return attMetadata.OptionSet.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
    
        }
    


    Regards,

    Abby

  • Community Member Profile Picture
    on at

    Hi Abby,

    It worked like a charm, buddy.!!

    Thank you very much :)

    Sandip

  • michael raam Profile Picture
    on at

    Hi,

    I can use only FetchXML with out any code around it, how would it be possible to retrieve the option set values. Right now I'm getting only the ID.

    Tnx,

    Micahel

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans