Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

Posted on by Microsoft Employee

Hi, i have a big  issue with Entities attribute values fetching..all the attributes have their own type which are almost all a pain in the ass to cast as String (and i don't talk about Entity References, which will make me perform extra queries just for them..which is totally insane, and a so old fashioned ) But i'm wondering: is there a way so i can avoid all this pain of checking the type of each attribute and perform the proper casting/conversion on it, like some wizard trick that would magically convert my entity attributes list to a Dictionary<string, string> ??? Honestly, i think that this API is 100% pure crap..please prove me wrong and show me that it is a little less crappy than i thought !

*This post is locked for comments

  • Suggested answer
    Syed Ibrahim Profile Picture
    Syed Ibrahim 6,257 on at
    RE: How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

    Please follow the method if you want all the fields to be retrieved as string be try like below.

    Entity _crmConfig = service.Retrieve("new_crmconfigurations", prop_EntityIdGuid, new ColumnSet("new_emailsubject", "new_emailbody"));

                   string subject = _crmConfig.Attributes.Contains("new_emailsubject") ? (string)_crmConfig.Attributes["new_emailsubject"] : string.Empty;

                   string mailbody = _crmConfig.Attributes.Contains("lib_emailbody") ? (string)_crmConfig.Attributes["new_emailbody"] : string.Empty;

                   string tempbody = mailbody;

                   string queue_guid_value = string.Empty;

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

    you can create one method which will do that work for you all the time :

    public class AttributeComponent
        {
            public string LogicalName { get; set; }
            public string AttributeType { get; set; }

            public string Value { get; set; }
        }


    public static List<AttributeComponent> ConvertAttributesToStringList(Entity entity)
            {
                List<AttributeComponent> attributes = new List<AttributeComponent>();
                string value = string.Empty;
                foreach (var attr in entity.Attributes)
                {
                    Type type = attr.Value.GetType();
                    switch (type.Name)
                    {
                        case "EntityReference":
                            value = ((EntityReference)entity.Attributes[attr.Key]).Name;
                            attributes.Add(new AttributeComponent { AttributeType = type.Name,LogicalName =attr.Key,Value= value });
                            break;
                        case "String":
                            value = (string)entity.Attributes[attr.Key];
                            attributes.Add(new AttributeComponent { AttributeType = type.Name, LogicalName = attr.Key, Value = value });
                            break;
                            //and so on
                    }                            
                }
                return attributes;
            }
        }

       

  • xRM Edge Profile Picture
    xRM Edge on at
    RE: How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

    You might want to look into the Early Bound programming paradigm in which we generate proxy classes beforehand. That enables us to reference all the attributes of an entity by their name with their proper type built-in.

    In the SDK there is a utility called CrmSvcUtil that generates these classes.

    I personally use a Visual Studio add-on called XrmToolkit to do this, but there are other free tools to help with this: 

    github.com/.../Early-Bound-Generator

    There are other developer add-ons you might find interesting, including Jason Lattimer's:

    https://marketplace.visualstudio.com/items?itemName=JLattimer.DynamicsCRMDeveloperExtensions

    And here's a blog that compared Early Bound vs. Late Bound performance:

    https://woodsworkblog.wordpress.com/2013/02/25/crm-2011-early-binding-vs-late-binding-performance/

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

    It does not matter if it's crap or not - it's simply what you need to learn if you want to work with Dynamics:)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to cast/convert all the attribute values of an entity to String, without having to check attr type ?

    There isn't really any wizard move for that afaik.

    Learn the Web API is my advice. Learn Azure and ADAL too if you need to.

    Otherwise, you can minimise the amount of code needed by creating workflows within dynamics.

    Workflows do the casting for you:

    For example, you can create text fields in the CRM for the fields you want to copy over and make them invisible on the form. A workflows can be set to fire on fields changing to populate strings with their attributes.

    eg. You have a field called new_someEntity which references an entity object.

    A workflow fires on change of this field and copies one of it's fields into a text field string called new_str_someEntity

    The form assistant on the right hand side of the workflow editor can help you see what's possible.

    In the workflow you're editing, the string field would then look something like this:

    Some Entity String: {fieldIwant(relationship (SomeEntity))}

    You don't have to write the above declaration as I said. The form assistant does it for you as you click and select the values you want.

    But honestly, I recommend you play around with Lattimer's CRM REST Builder, to get the hang of Web API queries:

    https://github.com/jlattimer/CRMRESTBuilder/releases

    It works fine in my CRM 365 instance.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans