Search code examples
c#entity-frameworkentitydynamics-crm

Find Dynamics Contact Entity by value of field in C#


I know that I can retrieve a contact by Guid like this:

var connection = new CrmConnection("CRM");
var service = new OrganizationService(connection);

var contact = service.Retrieve("contact", new Guid("ee07d029-e445-e511-8118-c4346bad4044"), new ColumnSet() { AllColumns = true });

...But that requires me to have the Guid of the contact. What if I want to find it by emailaddress1 for instance?

I'm new to Dynamics and I've tried looking at the documentation and googling it without luck. I'm using the Online (hosted) version of Dynamics.

Any help would be greatly appreciated.


Solution

  • Something like this:

    QueryByAttribute query = new QueryByAttribute("contact");
    query.ColumnSet = new ColumnSet("fullname", "emailaddress1");
    query.Attributes.AddRange("emailaddress1");
    query.Values.AddRange("[email protected]");
    EntityCollection retrieved = service.RetrieveMultiple(query);
    

    Sample: Retrieve multiple with the QueryByAttribute class