Saturday, November 22, 2008

Get attributes display name into DropDownList in CRM 4.0

In CRM 4.0 if you want the label name of a field also called attribute display name, you can get it by the Metadata Service.

Here is the code:

MetadataService service = GetMetadataService(crmServerUrl, orgName);

try
{
RetrieveEntityRequest req = new RetrieveEntityRequest();
req.EntityItems = EntityItems.IncludeAttributes;
req.LogicalName = "new_employee"; // enter the name of the entity
RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(req);

foreach (AttributeMetadata attribute in res.EntityMetadata.Attributes)
{
ListItem item = new ListItem();
item.Value = attribute.LogicalName;
item.Text = attribute.DisplayName.LocLabels[0].Label;
DropDownList1.Items.Add(item);
}
DropDownList1.DataBind();
}
catch{}


if you want only specific fields you can add in the foreach statment

if (attribute.LogicalName == "new_fullname") \\ where new_fullname is your field's name

Hope this was useful

Tomer

No comments: