Dremor
Dremor

Reputation: 831

How to retrieve addresses using the odata endpoint

I'm trying to retrieve an address on a standard entity.

the request is the following :

https://mycrm.api.crm4.dynamics.com/api/data/v9.1/contacts(guid)

In this example, I get all fields, but the ones that is of interest to me is address1_addressid, which seems to be a Guid, referencing some of record, but I cannot find it in the "Many to One" relationship list I retrieved using the following command :

https://mycrm.api.crm4.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='contact')?$select=LogicalName&$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)

I would like to retrieve those addresses in a generic manner, as I'm working on a generic NetStandard 2.0 library. I won't be able to know on which entity I'll be working, and thus won't be able to hard-code a list of addresses field names.

Upvotes: 0

Views: 1333

Answers (1)

Aron
Aron

Reputation: 3935

Here's one way to find the Contact to Address relationship:

https://myOrg.api.crm.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='customeraddress')?$select=LogicalName&$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)

AddressRelationship

I got an error with https://myOrg.api.crm.dynamics.com/api/data/v9.1/contacts(guid)?$expand=address1_addressid.

I looked into the MetaData and discovered that address1_addressid has a type of Primary Key:
PrimaryKey

While a normal lookup field has a type of Lookup: Lookup

Considering the error message that appears when attempting to expand address1_addressid, I think the issue is address1_addressid's data type.

Property 'address1_addressid' on type 'Microsoft.Dynamics.CRM.contact' is not a navigation property or complex property. Only navigation properties can be expanded.

It would seem that rather than using $expand to get the address details, you'd have to make a separate call for it:
https://myOrg.api.crm.dynamics.com/api/data/v9.1/customeraddresses(guid)

UPDATE
By reviewing the full Contact-Address relationship, via this query: https://myOrg.api.crm.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='customeraddress')?$select=LogicalName&$expand=ManyToOneRelationships.

I discovered the ReferencedEntityNavigationPropertyName was set to Contact_CustomerAddress.

fullRelationship

The error message before talked about the Navigation property so I gave it a shot. Using that property named allowed me to expand the Address info from the Contact:

https://myOrg.api.crm.dynamics.com/api/data/v9.1/contacts(guid)?$expand=Contact_CustomerAddress

Interestingly, expanding that navigation property returns all 3 customer addresses:
Addresses

Upvotes: 2

Related Questions