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)

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

I have the same question (0)
  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Hi

    You have to use RetrieveAttributeRequest to get the details of an attribute.

    Please refer the following:

    msdn.microsoft.com/.../microsoft.xrm.sdk.messages.retrieveattributerequest.aspx

  • Pravesh Rai Profile Picture
    on at

    I am using that code for that you modified it that you are saying

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

    FieldLogical is already define and entity is Account.

    now tell me you suggestion

  • Suggested answer
    M.Azwar Alam Profile Picture
    on at

    Hi

    check this it might help you stackoverflow.com/.../how-to-get-field-types-in-ms-crm-2011

    Marsk as answer verified, it it works for you

  • Pravesh Rai Profile Picture
    on at

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

  • Nithya Gopinath Profile Picture
    17,078 on at

    Hi Pravesh,

    You could write Javascript code to get the attribute type.

    Xrm.Page.getAttribute("attributename").getAttributeType();
  • Pravesh Rai Profile Picture
    on at

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

  • gupta.ambar2009@gmail.com Profile Picture
    797 on at

    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()

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    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);

  • Pravesh Rai Profile Picture
    on at

    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

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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); }

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 March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans