How to retrieve entity names in CRM 2016

2019-07-12 02:39发布

问题:

In our solution, we are building HTTP/ODATA requests dynamically.

For example, we will build a URL that looks like this:

[http://org....api/v8.1/]accounts(00000000-0000-0000-0000-000000000001)/primarycontactid?$select=fullname

How do we dynamically get a list of all the entities such as 'accounts'?

In 2011, we would simply execute against LeadSet/AccountSet/etcSet, what is the strategy in 2016?

回答1:

I don't know if there is some "language" trick (obviously the name of the set is just plural name in english of the entity, but that's not good enough for me), so I it like that - simply call webAPI metadata:

http://[crmurl]/api/data/v8.2/EntityDefinitions?$select=EntitySetName,LogicalName&$filter=LogicalName eq 'account'

result is the following:

{
  "@odata.context":"http://[crmurl]/api/data/v8.2/$metadata#EntityDefinitions(EntitySetName,LogicalName)","value":[
    {
      "EntitySetName":"accounts","LogicalName":"account","MetadataId":"70816501-edb9-4740-a16c-6a5efbc05d84"
    }
  ]
}

So you get the idea. Of course you can simply skip the $filter part and simply get list of all set names and cache them somewhere.



回答2:

1.To get all the entities available in your crm:

https://<your org name>.crm.dynamics.com/api/data/v8.2/

2.To get all the records metadata information (Retrieve Multiple Records):

https://<your org name>.crm.dynamics.com/api/data/v8.2/accounts?

3.To get the specific record email address information (Retrieve Single Record). What ever we chose the attributes in the select clause, we can get those attributes information in the result:

https://<your org name>.crm.dynamics.com/api/data/v8.2/accounts?$select=emailaddress1&$filter=accountid eq <GUID goes here>

4.To get entire metadata information in the CRM:

https://<your org name>.crm.dynamics.com/api/data/v8.2/EntityDefinitions


回答3:

Another approach would be to generate the plural name from the singular name.

As far as I understand, the rules to pluralize entity names in the Web API v8+ are:

  • ends with s, x, z, ch, or sh: add 'es'
  • ends with y: remove 'y', add 'ies'
  • else: add 's'

Here is a JavaScript function that I use for this:

function pluralName (name) {
    var plural = '';
    if (name != null && typeof(name) == 'string') {
        var len = name.length;
        var lastChar = len > 0 ? name.slice(-1) : '';
        var last2Chars = len > 1 ? name.slice(-2) : '';

        if (lastChar == 's' || lastChar == 'x' || lastChar == 'z' || last2Chars == 'ch' || last2Chars == 'sh') {
            plural = name + 'es';
        }
        else if (lastChar == 'y') {
            //strip off last character and add suffix
            plural = name.substr(0, len - 1) + 'ies';
        }
        else {
            plural = name + 's';
        }
    }
    return plural;
}


回答4:

Well, if you want the entities list, you can simply query and parser the root of the service like this:

https://contoso.api.crm.dynamics.com/api/data/v8.1/

If you want the fields too, you can do this:

https://contoso.api.crm.dynamics.com/api/data/v8.1/$metadata