Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get option set label from fetchXML aggregate query.

Posted on by Microsoft Employee

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

  • michael raam Profile Picture
    michael raam on at
    RE: Get option set label from fetchXML aggregate query.

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get option set label from fetchXML aggregate query.

    Hi Abby,

    It worked like a charm, buddy.!!

    Thank you very much :)

    Sandip

  • Suggested answer
    Abby Kong Profile Picture
    Abby Kong 6 on at
    RE: Get option set label from fetchXML aggregate query.

    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
    Community Member Microsoft Employee on at
    RE: Get option set label from fetchXML aggregate query.

    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.

               }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans