Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to get Attribute type of any field

(1) ShareShare
ReportReport
Posted on by

Hello,

I want a simple code that shows me how to get attribute type of any field.

Example: If a field is Lookup then i get as a result "lookup" OR a field is an Optionset then i get "Optionset".

I used this code but this line show me only type of field like System.Int32 and all

I want code in C#

Thanks 

Pravesh Rai

*This post is locked for comments

  • Suggested answer
    MMK Profile Picture
    745 on at
    RE: How to get Attribute type of any field

    Hi I have made a simple solution for that works for me without getting the metadata

    all i need to do is pass the the entity and the attribute name to get attributes type

    private string GetAttibuteType(Entity entity, string attributeName)

           {

               try

               {

                   if (entity.Attributes.Contains(attributeName))

                   {

                       string type = entity.Attributes[attributeName].GetType().Name;

                       return type;

                   }

                   return null;

               }catch(Exception ee)

               {

                   return null;

               }

           }

    hope this help

  • Community Member Profile Picture
    on at
    RE: How to get Attribute type of any field

    I Did

    public static string GetAttributeLabel(IOrganizationService service, Entity entity, string attribute)
            {
                string strLabel = String.Empty;
                OptionSetValue option = null;
    
                var attributeRequest = new RetrieveAttributeRequest
                {
                    EntityLogicalName = entity.LogicalName,
                    LogicalName = attribute,
                    RetrieveAsIfPublished = true
                };
                
                RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)service.Execute(attributeRequest);
                AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;
                OptionMetadataCollection optionMeta = null;
    
                //Console.Write("\tDebug attributeType : " + attrMetadata.AttributeType.ToString() + "\t");
    
                #region Switch
                switch (attrMetadata.AttributeType.ToString())
                {
                    case "Status": //Status
                        optionMeta = ((StatusAttributeMetadata)attrMetadata).OptionSet.Options;
                        break;
                    case "Picklist": //Picklist
                        optionMeta = ((PicklistAttributeMetadata)attrMetadata).OptionSet.Options;
                        break;
                    case "State": //State
                        optionMeta = ((StateAttributeMetadata)attrMetadata).OptionSet.Options;
                        break;
                    case "Decimal": //Decimal
                        break;
                    case "Enum": //Enum
                        break;
                    case "Memo": //Memo
                        break;
                    case "Money": //Money
                        break;
                    case "Lookup":
                        break;
                    case "Integer":
                        break;
                    case "Owner":
                        strLabel = ((EntityReference)entity.Attributes[attribute]).Name;
                        break;
                    case "DateTime": //DateTime
                        strLabel = Convert.ToDateTime(entity.Attributes[attribute].ToString()).ToString();
                        break;
                    case "Boolean": //Boolean
                        break;
                    case "String": //String
                        strLabel = entity[attribute].ToString();
                        break;
                    case "Double": //Double
                        break;
                    case "EntityName": //Entity Name
                        break;
                    case "Image": //Image, it will return image name.
                        break;
                    case "BigInt":
                        break;
                    case "ManagedProperty":
                        break;
                    case "Uniqueidentifier":
                        break;
                    case "Virtual":
                        break;
                    default:
                        //TODO: Write Err Exception
                        break;
                }
                #endregion Switch
    
                //If this attr is OptionSet, Find Label
                if (optionMeta != null)
                {
                    option = ((OptionSetValue)entity.Attributes[attribute]);
                    foreach (OptionMetadata metadata in optionMeta)
                    {
                        if (metadata.Value == option.Value)
                        {
                            strLabel = metadata.Label.UserLocalizedLabel.Label;
                        }
                    }
                }
    
                return strLabel;
            }


  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at
    RE: How to get Attribute type of any field
    public static void Run(IOrganizationService orgService)
            {
                //get single attribute metadata
                RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
                {
                    EntityLogicalName = "account",//Contact.EntityLogicalName,
                    LogicalName = "primarycontactid",
                    RetrieveAsIfPublished = false
                };
                
                // Execute the request
                RetrieveAttributeResponse attributeResponse =
                    (RetrieveAttributeResponse)OrgService.Execute(attributeRequest);
    
                Console.WriteLine("Retrieved the attribute {0}.",
                    //attributeResponse.AttributeMetadata.SchemaName
                    attributeResponse.AttributeMetadata.AttributeType
                    );
            }


    Hi Pravesh

    If you use

    Type TYP = (ObjEntity.Attributes[FieldLogical]).GetType();

    In this

    ObjEntity.Attributes[FieldLogical] would give you the attribute value. From this you can only find the c# type of this value.

    If you want to get the attribute metadata like Lookup optionset or so,

    then you would have to use different query, please use the link I have given you already.

  • Verified answer
    Nithya Gopinath Profile Picture
    17,076 on at
    RE: How to get Attribute type of any field

    Hi Pravesh,

    Try the code below.

    RetrieveEntityRequest req = new RetrieveEntityRequest();
     
       req.RetrieveAsIfPublished = true;
     
       req.LogicalName = "account";
     
       req.EntityFilters = EntityFilters.Attributes;
     
       RetrieveEntityResponse resp = (RetrieveEntityResponse)service.Execute(req);
       EntityMetadata em = resp.EntityMetadata;
    foreach (AttributeMetadata a in em.Attributes) {
    if(a.SchemaName == "yourattributename") Console.WriteLine(a.AttributeType); }
  • Pravesh Rai Profile Picture
    on at
    RE: How to get Attribute type of any field

    Hello Ambar,

    This code showing an error in foreach like cannot convert type'system.collection.generic.keyvaluepair<string, object> to microsoft.xrm.sdk.metadata.attributemetadata

    can you tell me why its showing this error

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to get Attribute type of any field

    Please use AttributeMetadata.AttributeType to get attribute type.

    msdn.microsoft.com/.../microsoft.xrm.sdk.metadata.attributemetadata.aspx

    Console.WriteLine("Retrieved the attribute {0}.",

       attributeResponse.AttributeMetadata.AttributeType);

  • gupta.ambar2009@gmail.com Profile Picture
    797 on at
    RE: How to get Attribute type of any field

    Hello Pravesh ,

    you have to take you code in foreach like below

    foreach(AttributeMetadata allattributes in  currentEntity.Attributes)

    {

       //to get attribute type

    string type=   allattributes.AttributeType.Value.ToString()

    }

  • Pravesh Rai Profile Picture
    on at
    RE: How to get Attribute type of any field

    I want ans in C# not in Javascript. Getting it

  • Nithya Gopinath Profile Picture
    17,076 on at
    RE: How to get Attribute type of any field

    Hi Pravesh,

    You could write Javascript code to get the attribute type.

    Xrm.Page.getAttribute("attributename").getAttributeType();
  • Pravesh Rai Profile Picture
    on at
    RE: How to get Attribute type of any field

    Mr. Alam, i think you didn't get my question

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February 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... 293,001 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,833 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans