简体   繁体   中英

getting error while fetching fields from Dynamics CRM(Online Sales) from different server location

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.

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;
            }
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM