MS CRM – Get Attribute metadata

Some time we need to get attribute metadata. for example we want data type of attribute dynamically,

public static string GetAttributeMetadata(Entity entity, string attributeName)
{
// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entity.LogicalName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};

// Execute the request
RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)CRMConnection._organizationService.Execute(attributeRequest);

string type = attributeResponse.AttributeMetadata.AttributeType.ToString();

return type;
}

and if it is lookup then

public static string GetLookupType(Entity entity, string attributeName)
{
// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entity.LogicalName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};

// Execute the request
RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)CRMConnection._organizationService.Execute(attributeRequest);

string EntityType = ((LookupAttributeMetadata)(attributeResponse.AttributeMetadata)).Targets[0];
return EntityType;
}

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s