从不同服务器位置从Dynamics CRM(在线销售)获取字段时出错

时间:2018-01-25 12:19:06

标签: c# dynamics-crm microsoft-dynamics dynamics-crm-online dynamics-crm-2016

我正在尝试从组织中获取所有帐户实体字段。(https://democrm365.crm4.dynamics.com)。

另外,我在帐户中创建了一些自定义字段并添加到表单中。之后,当我运行以下代码时,我将获得与帐户相关的所有字段。

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

同样,我使用上述所有步骤在不同的组织(https://zoho5.crm.dynamics.com)上测试了相同的代码 下面的代码不起作用。

if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)

attribute.DisplayName.UserLocalizedLabel对于所有字段都为空,例如(帐户名,帐号等)

在一些测试运行后,我从帐户表单中删除了自定义字段并发布了表单。然后上面的代码工作正常。

1 个答案:

答案 0 :(得分:0)

试试这个。当LocalizedLabels为空时,您可能必须从UserLocalizedLabel获取标签。

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