Fetching $metadata for a specific collection or en

2019-02-20 01:21发布

问题:

The environment

As part of an integration project, I need a PHP website to be able to both read from and write to Microsoft Dynamics NAV 2016's Odata services.

Microsoft Dynamics NAV 2016 uses the Odata v3 standard.


The context

Now, let's take my customer service as an example.

Fetching a collection of entities

Fetching an overview of my collection of customer entities works fine, using a link with the following format :

https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer

Fetching a single entity

Fetching a single customer entity based on id works fine, using a link with the following format :

https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'<Id>\')

Fetching the global metadata

Getting an overview of the $metadata for all of my services works fine (although it's lacking Json support), using a link with the following format :

https://<Server>:<WebServicePort>/<ServerInstance>/OData/$metadata

Fetching collection-specific metadata

Now, based on both the Odata v3 specs and the value of the odata.metadata property of my customer overview service, I would expect to be able to get an overview of the $metadata of the customer entity, using a link with the following format :

https://<Server>:<WebServicePort>/<ServerInstance>/OData/$metadata#customer

This doesn't work as expected. See sub-section The issue below.

Fetching entity-specific metadata

Similarly, based on both the Odata v3 specs and the value of the odata.metadata property of my dataset when retrieving a single customer, I would expect to be able to get an overview of the $metadata of a single field of the customer entity, using a link with this format :

https://<Server>:<WebServicePort>/<ServerInstance>/OData/$metadata#customer/@<FieldName>

This doesn't work as expected. See sub-section The issue below.

The issue

For some reason, everything following $metadata appears to be ignored. This means that the last three URLs give me the exact same output, which is not what I expected.


The actual question(s)

  • Is it possible to fetch the metadata of just one collection or entity, as I'm trying in sub-sections Fetching collection-specific metadata and Fetching entity-specific metadata of my The context section hereabove?
  • If yes, what I doing wrong here? What am I missing?

回答1:

As @xuzhg suggested in the comments and as is indicated by Github issues like Support Metadata as a service (#181), it appears that the Odata $metadata are not really a dynamic service. Instead, it's just a single XML file.

This explains not only why anything after $metadata is ignored in links of format https://<Server>:<WebServicePort>/<ServerInstance>/OData/$metadata#..., but also why it only supports XML, and not Json (unlike actual Odata services).



回答2:

Since the specific metadata you want is identified by a fragment appended to the metadata URI, you must fetch the entire $metadata document and then dereference the fragment on the client.

The good news is that the fragment dereferencing is pretty straightforward. A fragment like #customer specifies an EntityType element whose Name attribute has the value customer. Similarly, #customer/@someprop maps to a Property element whose Name attribute is someprop.