Product/components used and version/fix level:
Cumulocity 10.17
Detailed explanation of the problem:
I’m looking for a way to query and get a count of total devices in a subtenancy. I know I can query inventory/managedObjects?fragmentType=c8y_IsDevice&withTotalPages=True&pageSize=100 and get the totalPages, navigate to pageNumber= in the query string and iterate through and count the devices in that page and then multiply the pagesize * (totalPages-1) + devices counted in the last page. but that seems really fiddly and wonder if there’s a parameter to the inventory to merely return a count.
I didn’t see anything in https://cumulocity.com/api/core/10.17.0/#tag/Managed-objects
Error messages / full error message screenshot / log file:
No error message
Question related to a free trial, or to a production (customer) instance?
Related to a hosted cloud production instance
Hi,
you can add withTotalElements=true to your query. Then your response will contain a statistics.totalElements field then:
{
"managedObjects": [
{ ... }, { ... }, { ... }, { ... }, { ... }
],
"next": "https://examples.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=5&fragmentType=c8y_IsDevice¤tPage=2",
"self": "https://examples.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=5&fragmentType=c8y_IsDevice¤tPage=1",
"statistics": {
"currentPage": 1,
"pageSize": 5,
"totalElements": 83245 <= total count of query results
}
}
It’s listed as API parameter for the retrieve all managed objects endpoint.
Alternatively, if it’s just about the count, you could set your pageSize to 1 so that totalPages=totalCount. Also, when stating withTotalPages the pageSize shouldbe part of the statistics fragment - so you wouldn’t need to extract that from the URL. But I think withTotalElements is the parameter you’re looking for.
Ah, there it is! Yes, I find it now. Thanks for pointing that out!
You could also do pageSize=1 - the the returned totalPages indicates the number of devices.