More REST queries with CRM 2011

Prompted by a question on a previous post, I’ve posted some additional examples on the CRM 2011 REST interface. In particular, this post covers the ability to query relationships and complex types.  All the examples below and more are explained in the CRM 2011 SDK.

From an <entity>Set query, there are a number of <link …>  elements that describe the URL’s to access information from the current entity relationships. For example:

To list a specific Account, request


If you then search in the response content for “contact_customer_accounts”, you will find an entry like

<link rel=”” type=”application/atom+xml;type=feed” title=”contact_customer_accounts” href=”AccountSet(guid’b9436b0d-b832-e011-9e40-00155da9cd4f’)/contact_customer_accounts” />

The href attribute presents a URL to retrieve the results from the contact_customer_accounts relationship which describes the 1:N between Account and Contact.  So, if we execute that request, we receive all Contacts under that account.

Likewise, from a specific Contact


The linked Account can be found using the same relationship name


Furthermore, you can also add filters to these requests in the usual way, so looking for the Contact ‘Fred Bloggs’ under known Account

xrmservices/2011/organizationdata.svc/AccountSet(guid’b9436b0d-b832-e011-9e40-00155da9cd4f’)/contact_customer_accounts?$filter=FullName eq ‘Fred Bloggs’

When I said it’s not always intuitive, it would be nice to be able to filter the entity attributes directly such as

xrmservices/2011/organizationdata.svc/ContactSet?$filter=ParentCustomerId eq ‘b9436b0d-b832-e011-9e40-00155da9cd4f’

and you’re left with a CRM error

Operator ‘eq’ incompatible with operand types ‘Microsoft.Crm.Metadata.ComplexTypeInstance`1[[Microsoft.Xrm.Sdk.EntityReference, Microsoft.Xrm.Sdk, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]’ and ‘System.String’ at position 17

But theres a solution! Complex Types can be handled by specifying the attribute in the LHS of the filter expression such as

xrmservices/2011/organizationdata.svc/ContactSet?$filter=ParentCustomerId/Id eq ‘b9436b0d-b832-e011-9e40-00155da9cd4f’

Here’s some more examples based on the Account->Contact->Address relationships. You can view it in a spreadsheet here.