How to retrieve display name of an attribute in Dynamics CRM using C#

Recently, i got a requirement to get the display name of attributed of an entity in C# code. This was basically a part of building an application to retrieve the Name and option set values of all option sets of any selected entity.

Anyway, i’ll update about that later when the application is finished but for now i feel sharing the code to retrieve the Label name of attributes which can give you the label of attribute if you pass it the schema name of entity, schema name of attribute and Iorganisation service object.

Class to include:

Write  “using Microsoft.Xrm.Sdk.Messages;” to include the same.

 

Code Snippet:

///////////////////////////////////////////////////////////////////////////

public static string RetrieveAttributeDisplayName( string EntitySchemaName, string AttributeSchemaName, IOrganizationService service)
 {

RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
 {
 EntityLogicalName = EntitySchemaName,
 LogicalName = AttributeSchemaName,
 RetrieveAsIfPublished = true
 };
 RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
 AttributeMetadata retrievedAttributeMetadata = (AttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

//Get Length
 // Int32 length = (Int32)((Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata)retrievedAttributeMetadata).MaxLength;
 return retrievedAttributeMetadata.DisplayName.UserLocalizedLabel.Label;
 }

///////////////////////////////////////////////////////////////////////////

Hope the post will be helpful.

Thanks

 

 

 

 

One thought on “How to retrieve display name of an attribute in Dynamics CRM using C#

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: