0
votes

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.

1
Did you get a chance to test my code?Arun Vinoth - MVP
partially yes, i used your code, but i am unable to regenerate error, so i am not sure is it working or notVirendra Yadav
what do you mean? Did you try to debug?Arun Vinoth - MVP
i applied the code, and debugging with same scenario but attribute.DisplayName.UserLocalizedLabel is always contains value, so my condition is working fine, need some more testingVirendra Yadav

1 Answers

0
votes

Try this. You may have to get the label from LocalizedLabels when UserLocalizedLabel is null.

foreach (AttributeMetadata attribute in currentEntity.Attributes)
{    
     if (attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
     {
            string attributeName = attribute.LogicalName;

            if (attribute.DisplayName.UserLocalizedLabel != null)
            {
                attributeName = attribute.DisplayName.UserLocalizedLabel.Label;
            }

            if (attributeName == attribute.LogicalName && attribute.DisplayName.LocalizedLabels.Count > 0)
            {
                attributeName = attribute.DisplayName.LocalizedLabels[0].Label;
            }
     }
}