We want to enable the client to post to an endpoint such as:
[Route("Account", Name = "CreateAccount", Order = 1)]
[HttpPost]
public Account CreateAccount([FromBody] Account account)
{
var newAccount = _accountService.CreateAccountEntity(account);
return newAccount;
}
We know that this can be done:
POST [Organization URI]/api/data/v8.2/accounts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
"name": "Sample Account",
"creditonhold": false,
"address1_latitude": 47.639583,
"description": "This is the description of the sample account",
"revenue": 5000000,
"accountcategorycode": 1
}
How do we expose to the consumer the requirements for each post/put?
To phrase it in different words, if I need to update a record on a custom or base entity using Web API as provided by CRM 2016, how do I know which fields are required to create or update the entity?
Edit: I've attempted Hank's approach, and this didn't return any metadata on the entity:
You can query the Dynamics 365 metadata using the WebApi endpoint, as shown in the SDK.
For example, to retrieve all the attributes (which includes the requirement level) for
account
entity:In order to get all the metadata for an entity using SOAP endpoint, you can use RetrieveEntityRequest:
EntityFiters is an enum which allows you to specify what metadata are you trying to get:
This is a flag enum so you can use it like that:
Or simply use the
All
value to get all necessary metadata. In your attempt you failed to retrieve metadata, because you asked only for Entity metadata and you are interested in Attributes metadata.So, taking your code snippet as a base, I would use this in the following way:
Method
GetEntityMetadata
is simply doing what was in previous example, so callingRetrieveEntityRequest
and returningRetrieveEntityResponse
. Of course the implementation of methodCheckIfValueIsProvided
depends on how your Account model class is defined, but probably you will need some kind of mapping between your model and CRM Entity model (to know how to map for example field "accountnumber" to some field in your model). This is far beyond the scope of this question, but I believe that you already know enough to get started. Just remember, that this is only an example. You should not keep this logic inside your controller class, you should move it to some utility class which you can reuse in different controllers. Metadata does not change often (and you probably have control over this changes), so you also probably would like to cache the metadata somewhere in your web application etc. I hope that you already have an idea what can be done, but the whole design if the logic is another story.If you want to do this from JavaScript you should probably stick to the webAPI:
Will get you what you want (name of the attribute and its Required level). It will look like that:
You can use this to validate the request on client side, to give user a hint that he is missing important data before he sends a request to the server.
You can use the
RetrieveEntityRequest
to get the metadata for an entity.In the following example the metadata for entity Account is retrieved:
The response object contains an
EntityMetadata
property. In it you can find the requirement setting of an attribute like this: