title: Getting all related Contact for an Account entity in Dynamics 365 CRM v9.0 web api
body: <p>Basically, I'm querying the D365 web API and I'm trying to get all of the related contacts for an account.</p>
  
  <p>Been following this link:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/query-data-web-api#retrieve-related-entities-by-expanding-navigation-properties" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/query-data-web-api#retrieve-related-entities-by-expanding-navigation-properties</a></p>
  
  <p>Trying to use <code>$expand</code> but it only will bring over <code>_primarycontactid_value</code>. So just the one primary contact and not everyone that is related to the Account.</p>
  
  <p>It would look something like the following:</p>
  
  <p><code>/api/data/v9.0/accounts&amp;?select=name&amp;$expand=Contacts(fullname, email)</code></p>
  
  <p>The only fields in Account that have "contact" in them are:</p>
  
  <pre><code>preferredcontactmethodcode
  _primarycontactid_value
  address2_primarycontactname
  address1_primarycontactname
  _tcc_primaryinvoicecontactid_value
  _tcc_consultingcontact_value
  _tcc_contactlist_value //some custom field that doesn't apparently do anything
  _new_foundationcontact_value
  _tcc_primaryapcontactid_value
  </code></pre>
  
  <p>So none of which can be used to look-up all of the contacts... that I know of.</p>
  
  <p>Another way to do it would be to start with the Contact first and then <code>$expand=</code> on the <code>_parentcustomerid_value</code>. But I need to filter on the Account to specify certain accounts that I want... this would just bring over every account and be incredibly slow. I'm not sure there is a way to <code>$filter=</code> on an <code>$expand=</code> value.</p>
  
  <p>So:</p>
  
  <ol>
  <li>How can I query the Account and all the related to Contacts for an Account?</li>
  <li>If there is no way, is it possible to use <code>$filter=</code> on and <code>$expand=</code> value?</li>
  </ol>
  
  <p>Trying to keep the amount of queries to a minimum. This could be solved by doing multiple iterative queries, but that will just make it incredibly slow. Or just query everything and piece it together, but that will be slow as well.</p>