4

我正在尝试从 CRM 安装中提取信息,到目前为止,这对于使用默认字段来说很好。但是,我在检索自定义字段时遇到了困难,例如联系人有一个名为 web_username 的自定义字段。

我目前的代码是

        QueryExpression query = new QueryExpression();
        query.EntityName = "contact";
        ColumnSet cols = new ColumnSet();
        cols.Attributes = new string[] { "firstname", "lastname" };
        query.ColumnSet = cols;

        BusinessEntityCollection beReturned = tomService.RetrieveMultiple(query);
        foreach (contact _contact in beReturned.BusinessEntities)
        {
            DataRow dr = dt.NewRow();
            dr["firstname"] = _contact.firstname;
            dr["lastname"] = _contact.lastname;
            dt.Rows.Add(dr);
        }

如何在查询中包含自定义字段?我试过四处搜索,但还没有运气,但我可能搜索不正确,因为我不习惯 CRM 术语。

提前干杯!

4

2 回答 2

6

我已经能够解决这个问题了。如果它对其他人有用,这就是我所做的。除了我将自定义字段添加到 ColumnSet 之外,查询的设置与以前一样。

cols.Attributes = new string[] { "firstname", "lastname", "new_web_username" };

然后使用 RetrieveMultipleResponse 和 Request 并将 ReturnDynamicEntities 设置为 true

        RetrieveMultipleResponse retrived = new RetrieveMultipleResponse();

        RetrieveMultipleRequest retrive = new RetrieveMultipleRequest();
        retrive.Query = query;
        retrive.ReturnDynamicEntities = true;

        retrived = (RetrieveMultipleResponse)tomService.Execute(retrive);

如果我有更好的方法可以做到这一点,请仍然发表评论。

编辑 如果您投给联系人,请使用我原始问题中的示例

contact myContact = (contact)myService.Retrieve(EntityName.contact.ToString(), userID, cols);

然后您可以访问对象的属性

                phone = myContact.telephone1;
            password = myContact.new_password;

如果您更新您在 CRM 中添加的 CRM 网络参考自定义字段,则可用。

于 2009-06-30T10:28:49.090 回答
0
    public List<Entity> GetEntitiesCollection(IOrganizationService service, string entityName, ColumnSet col)
  {
                        try
                        {
                            QueryExpression query = new QueryExpression
                            {
                                EntityName = entityName,
                                ColumnSet = col
                            };
                            var testResult = service.RetrieveMultiple(query);
                            var testResultSorted = testResult.Entities.OrderBy(x => x.LogicalName).ToList();

             foreach (Entity res in testResultSorted)
                        {
                            var keySorted = res.Attributes.OrderBy(x => x.Key).ToList();
                            DataRow dr = null;
                            dr = dt.NewRow();
                            foreach (var attribute in keySorted)
                            {
                                try
                                {
                                    if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue")
                                    {
                                        var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service);
dr[attribute.Key] = valueofattribute;


                                        }
                                        else if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.EntityReference")
                                        {
                                            dr[attribute.Key] = ((Microsoft.Xrm.Sdk.EntityReference)attribute.Value).Name;
                                        }
                                        else
                                        {
                                            dr[attribute.Key] = attribute.Value;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Response.Write("<br/>optionset Error is :" + ex.Message);
                                    }
                                }
                                dt.Rows.Add(dr);
                            }
                 return testResultSorted;
                 }
                        catch (Exception ex)
                        {
                            Response.Write("<br/> Error Message : " + ex.Message);
                            return null;
                        }
            }

//这里我提到了另一个函数:

 var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service);

//这个函数的定义如下:

public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
    {
                 string AttributeName = attributeName;
        string EntityLogicalName = entityName;
        RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
        {
            EntityFilters = EntityFilters.All,
            LogicalName = entityName
        };
        RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
        Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
        Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
        Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;

        IList<OptionMetadata> OptionsList = (from o in options.Options
                                             where o.Value.Value == optionSetValue
                                             select o).ToList();
        string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
        return optionsetLabel;
    }
于 2017-05-30T08:21:01.537 回答