I am trying to fetch all account entity fields from an organization.(https://democrm365.crm4.dynamics.com).
Also, I created some custom fields in account and added into form. After that when I run the following code I am getting all fields related to account.
IOrganizationService service = (IOrganizationService)serviceProxy;
RetrieveEntityRequest request = new RetrieveEntityRequest()
{
EntityFilters = EntityFilters.Attributes,
RetrieveAsIfPublished = false,
LogicalName = "account"
};
RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(request);
EntityMetadata currentEntity = res.EntityMetadata;
foreach (AttributeMetadata attribute in currentEntity.Attributes)
{
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
{
if (_allowedFieldTypes.Contains(attribute.AttributeType.ToString().ToLower()))
{
EntityField ef = new EntityField();
ef.AttributeType = attribute.AttributeType.ToString() ?? "";
ef.DisplayName = attribute.DisplayName.UserLocalizedLabel.Label;
ef.IsCustomField = attribute.IsCustomAttribute ?? false;
ef.IsAllowUpdate = attribute.IsValidForUpdate ?? false;
ef.LogicalName = attribute.LogicalName;
if (attribute.AttributeType.ToString().ToLower() == "picklist")
{
PicklistAttributeMetadata pm = (PicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
else if (attribute.AttributeType.ToString().ToLower() == "virtual")
{
if (attribute.AttributeTypeName.Value == "MultiSelectPicklistType")
{
MultiSelectPicklistAttributeMetadata pm = (MultiSelectPicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
}
if (Add)
{
fieldLst.Add(ef);
}
}
}
}
Again, I tested the same code on different organization (https://zoho5.crm.dynamics.com) with all above mentioned steps, then below code is not working.
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
attribute.DisplayName.UserLocalizedLabel
is null for all fields like (Account Name, Account No. Etc)
After some test runs, I removed the custom fields from account form and publish form. Then the above code is working fine.