Useful Dynamics CRM 2016 Web API Queries - Part 1

John Towgood, 21 July 2017

Web API is a new platform feature of Dynamics CRM 2016 that exposes CRM data over OData (Open Data Protocol). Web API offers REST based endpoint which can be used across a wide variety of programming languages, platforms, and devices.

Since Web API is intended to replace existing SOAP base endpoint I started using the Web API feature in JavaScript web resources to query for data.

Here are some queries which I found useful:

Use the plural name of the entity when querying for data

image

When using the Entity Name as it is the result will be an error “Resource not found for the segment”.

GET    http://testserver/testorg/api/data/v8.0/mag_consumersetup?$select=mag_name&$filter=contains(mag_name,'Test')
     {
   "error":{
     "code":"","message":"Resource not found for the segment 'mag_consumersetup'.","innererror":{
       "message":"Resource not found for the segment 'mag_consumersetup'.","type":"Microsoft.OData.Core.UriParser.ODataUnrecognizedPath
        Exception","stacktrace":"   at
…………
}

Using the plural name of the Entity by added ‘s’ to form mag_consumersetups resolves the issue.

GET    http://testserver/testorg/api/data/v8.0/mag_consumersetups?$select=mag_name&$filter=contains(mag_name,'Test')
     {
   "@odata.context":"
http://testserver/testorg/api/data/v8.0/$metadata#mag_consumersetups(mag_name)","value":[
     {
       "@odata.etag":"W/\"1257828567\"","mag_name":"Test A","mag_consumersetupid":"e0b6ae92-4230-e711-80bf-00155d048d78"
     }
   ]
}

Querying a custom entity with Business Process Flow enabled

image

When querying for all field values of Entity records the error “Property 'stageid' is of an unrecognized EdmPropertyKind”.

GET    http://testserver/testorg/api/data/v8.0/mag_consumersetup?filter=contains(mag_name,'Test')
     {
   "error":{
     "code":"","message":"Property 'stageid' is of an unrecognized EdmPropertyKind. Entity mag_onboardingrequest has duplicate navigation property names. All property names (Navigation and Structural property) must be unique in an Entity ","innererror":{
       "message":"Property 'stageid' is of an unrecognized EdmPropertyKind. Entity mag_onboardingrequest has duplicate navigation property names. All property names (Navigation and Structural property) must be unique in an Entity ","type":"Microsoft.Crm.CrmHttpException","stacktrace":"   at Microsoft.Crm.Extensibility.OData.CrmODataEntityTypeSerializer.CreateSelectExpandNode(EntityInstanceContext entityInstanceContext)\r\n   at …………
}

Query only the fields you need by specifying a select statement and field names

GET    http://testserver/testorg/api/data/v8.0/mag_consumersetups?$select=mag_name&$filter=contains(mag_name,'Test')
     {
   "@odata.context":"
http://testserver/testorg/api/data/v8.0/$metadata#mag_consumersetups(mag_name)","value":[
     {
       "@odata.etag":"W/\"1257828567\"","mag_name":"Test A","mag_consumersetupid":"e0b6ae92-4230-e711-80bf-00155d048d78"
     }
   ]
}

Querying an Action that has multiple output parameter with a least one of type EntityReference

image

POST    http://testserver/testorg/api/data/v8.0/mag_TestMultipleOutput
     {
   "error":{
     "code":"","message":"Resource not found for the segment 'mag_TestMultipleOutput'.","innererror":{
       "message":"Resource not found for the segment 'new_TestMultipleOutput'.","type":"Microsoft.OData.Core.UriParser.ODataUnrecognized
       PathException","stacktrace":"   at
…..
}

Workaround is to change the ContactOutput type from EntityReference to String and return a json string representing the contact record.

image

POST    http://testserver/testorg/api/data/v8.0/mag_TestMultipleOutput
     {
   "@odata.context":"
http://testserver/testorg/api/data/v8.0/$metadata#Microsoft.Dynamics.CRM.mag_TestMultipleOutputResponse","StringOutput":
   "test","ContactOutput":"{ \"contactid\" : \"FA2A2CA9-B0FC-E211-A2E0-00155D0C8359\" }"
}

Use JSON.Parse to convert response from a Json string into a response object then JSON.Parse the ContactOutput string to obtain the contacted.

image