Web API Tips and Tricks

Web API provides a easy way to compose HTTP requests and quickly get the results from Dynamics 365. There are a lot of good tools out there like PostMan, REST Builder and XrmToolbox which makes it even easier to compose and test with Web API.

Below are some request I found useful when using Web API. The official page here should always be the first place to check for documentation on how to make different types of requests and it is well documented.

Total Count of Records

Until recently we had no way of getting the total number of records for an entity if it exceeded 5000 records. Typically we would have used the aggregate methods on the Fetch XML or page through the records.  But now we can use the RetrieveTotalRecordCount function to get total record count.

https://{orgname}.crm.dynamics.com/api/data/v9.1/RetrieveTotalRecordCount(EntityNames=['contact','account'])

Note that in the above example, I have provided more than one entity as the EntityNames parameter supports collection.

You could also use the aggregate and get the count but this has the 50000 record limit.

https://{orgname}.crm.dynamics.com/api/data/v9.1/{collectionname}?$apply=aggregate($count as RecordCount)

e.g. collectionname=contacts

For a complete list of functions available refer to this page

Calling Bound Actions

When invoking bound actions make sure that the URL for the request follow the format described here.

Let's look at an example

The above request is bound to the Order(salesorder) entity and accepts two parameters. The status is straightforward but let's look at OrderClose.

  • OrderClose :- If we just click on the Type property in the above picture or can look at the documentation for orderclose type. We can set properties like actualstart, actualend etc. But if we scroll down we will notice the salesorderid ┬ásingle-valued lookup property for the Order entity which is the one we would need to specify. Ignore the activityid as the OrderClose activity does not exist and will be created after our request is successful.

So now our request would look something like this

In the example below webapiurl would be https://{orgname}.crm.dynamics.com/api/data/v9.1/

Resource Not Found for Segment

This is the common error we see when making Web API requests where our URL is not correct or if one of our parameters in the request body is not valid(e.g. typos)

Make sure the URL you are using is valid and all the contents of the body are properly set. One common place where we would make the mistake would be when using the lookup properties.

I plan to keep this post updated with more examples, so stay tuned :)