简体   繁体   中英

Dynamics CRM 2015 Online: cannot retrive custom fields

The following code throws a KeyNotFoundException at WriteLine():

CrmConnection crmConnection = CrmConnection.Parse(_connectionString);
using (OrganizationService service = new OrganizationService(crmConnection))
{
    QueryExpression query = new QueryExpression
    {
        EntityName = "contact",
        ColumnSet = new ColumnSet("fullname", "new_customer_num", "new_is_customer"),
    };

    EntityCollection contacts = service.RetrieveMultiple(query);
    if (contacts.Entities.Count > 0)
    {
        foreach (var e in contacts.Entities)
        {
            System.Diagnostics.Debug.WriteLine(
                e.Attributes["fullname"].ToString() + "; " + 
                e.Attributes["new_is_customer"].ToString());
        }
    }
}

I've already added new_customer_num and new_is_customer (required) fields to Contact entity. If I deliberately misspell them, then FaultException is thrown at service.RetrieveMultiple(query); so I guess CRM "knows" my custom fields. Then why doesn't query result include them?

Well, to make it work, I had to add a condition:

QueryExpression query = new QueryExpression
{
    EntityName = "contact",
    ColumnSet = new ColumnSet("fullname", "ht_customer_num", "ht_is_customer"),
    Criteria = new FilterExpression
    {
        Conditions = 
        {
            new ConditionExpression
            {
                AttributeName = "new_is_customer",
                Operator = ConditionOperator.Equal,
                Values = { true }
            },
        },
    }
};

SDK Doc for ColumnSet says that query returns only non-null values.

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