Count rows using FetchXml

Use the fetch element boolean returntotalrecordcount attribute to specify that the result include a count of all the records that meet the filter criteria, up to 5000. Use this when retrieving paged results to estimate the total number of pages to display.

You can't apply the fetch element top attribute together with returntotalrecordcount.

The behavior you can expect depends on whether you are using the SDK for .NET or Web API.

When the returntotalrecordcount attribute value is true, the EntityCollection returned from the RetrieveMultiple method includes values for the following properties:

Property Description
TotalRecordCount The total number of records up to 5000; otherwise the value is -1.
TotalRecordCountLimitExceeded true if the results of the query exceeds the total record count; otherwise, false.

The (TotalRecordCountLimitExceeded or @Microsoft.Dynamics.CRM.totalrecordcountlimitexceeded) value is useful when you need to calculate how many more paged requests you need to send to get all the results when (TotalRecordCount or @Microsoft.Dynamics.CRM.totalrecordcount) equals 5000.

If your page size is less than the maximum and (TotalRecordCount or @Microsoft.Dynamics.CRM.totalrecordcount) is equal to or less than 5000, you can calculate how many more paged requests you must send to get all the records.

When (TotalRecordCountLimitExceeded or @Microsoft.Dynamics.CRM.totalrecordcountlimitexceeded) is true and (TotalRecordCount or @Microsoft.Dynamics.CRM.totalrecordcount) equals 5000, you can't perform this calculation.

Tip

If you want to get a total count of records with no filter, use the RetrieveTotalRecordCount message with either the Web API RetrieveTotalRecordCount Function or with the SDK for .NET RetrieveTotalRecordCountRequest class. The data retrieved will be from a snapshot within the last 24 hours.

Next steps

Learn how to optimize performance.