Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'

(0) ShareShare
ReportReport
Posted on by 35

Hi, i am trying to read the value of an OptionSetValue using the code below, but it is not working and it's throwing the error :

Error of Plugin: System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'.
at Microsoft.Xrm.Sdk.Entity.GetAttributeValue[T(String attributeLogicalName).

Any ideas?

                    if (entity.Attributes.Contains("addresstypecode"))
                    {
                        var address_type = entity.GetAttributeValue("addresstypecode").Value.ToString();
                        QueryExpression query = new QueryExpression("customeraddress");
                        query.ColumnSet = new ColumnSet("addresstypecode");
                        query.Criteria.AddCondition("addresstypecode", ConditionOperator.Equal, address_type);
                        EntityCollection collection = service.RetrieveMultiple(query);
 

  • Verified answer
    Pankaj Gogoi Profile Picture
    3,177 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'

    Hi Jamal,

    Can you try with this.

    var address_type = ((OptionSetValue)entity["addresstypecode"]).Value;

    Best Regards

    PG

  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'

    You shouldn't cast the OptionSetValue to a string. If you change line 3 to the following it should work

    var address_type = entity.GetAttributeValue<OptionSetValue>("addresstypecode").Value;

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans