Retrieve Records filtering on an Entity Reference field using OData

I ran into a problem today trying to retrieve records filtering on an Entity Reference field using the OData endpoint.  Thanks to Jim Daly for helping me. An Entity Reference field contains several properties and Id is one of the its properties.  Usually when you are filtering using OData, all you have to do is use the keyword $filter, the search field, operator and the value.

For example, to retrieve all the accounts where address1_city equal Redmond, you’ll do the following:

 /AccountSet?$filter=Address1_City eq 'Redmond' 

 

However to retrieve from an Entity Reference field is little bit different.  To access the properties in an Entity Reference field, you just need to use the forward slash “/” and then follow by the property name.

 

For example, to retrieve all the accounts where primarycontactid equal to {guid}, you have to do the following:

 

 AccountSet?$filter=PrimaryContactId/Id eq (Guid'567DAA8F-BE0E-E011-B7C7-000C2967EE46') 

I hope this help in your next project.

Comments

  • Anonymous
    March 05, 2011
    Nice blog. You might like to check out my CRM 2011 OData Query Designer on codeplex. Here is the blog post bingsoft.wordpress.com/.../crm-2011-odata-query-designer-crm2011 Cheers, Rhett

  • Anonymous
    January 11, 2012
    Hi Thanks for the Post i've tried to filter a product of an Opportunity Product via oData  based on  a productId but the Filtering is not Working, what am i doing wrong? here is how i did var filter = "/ProductSet?$select=DefaultUoMId&$filter=Id eq (Guid'9B9F869E-531F-E111-BDF8-BC305BD0C8E3')"; please help thanks

  • Anonymous
    June 20, 2012
    Gday, This works a treat and using RegardingObjectId/Id on an activity entity gives me the values I'd expect. Is there some reason I can't also have RegardingObjectId/LogicalName ? or is the property called something else? If I try filter by the entity name I just get a BadRequest Thanks

  • Anonymous
    July 28, 2015
    Any other operator will be supported for GUID?