Search code examples
c#restapidynamics-crmpostman

Is there a way to use join with OData in Dynamics CRM API?


Is there a way to turn this code into a CRM Dynamics API request?

var t = (from contact in lServiceContext.CreateQuery("contact")
             join account in lServiceContext.CreateQuery("account") on contact["parentcustomerid"] equals account["accountid"]
             where contact[key].Equals(value) && contact["statuscode"].Equals(1)
             select new
             {
                 AccountID = !account.Contains("accountid") ? string.Empty : account["accountid"],
                 AccountNumber = !account.Contains("accountnumber") ? string.Empty : account["accountnumber"],
                 AccountR3 = !account.Contains("new_r3number") ? string.Empty : account["new_r3number"]
             });

Well, i read this docs and i didn't quite understand how it works.

The idea is to make a request at the postman.


Solution

  • I believe something like this should do the trick:

    https://myOrg.crm.dynamics.com/api/data/v9.1/contacts?$select=contactid,fullname,_parentcustomerid_value&$expand=parentcustomerid_account($select=accountid,accountnumber,name,new_r3number)

    In this case it looks like you can get by with an OData query, but if you ever run up against the WebAPI's OData limitations, you can submit a FetchXML query.

    When building WebAPI queries, Jason Lattimer's REST Builder can be indispensible.

    And, in your original code it might be worth considering using the GetAttributeValue method.