There are couple of ways to gets entity record counts: you could use XrmToolBox’s Record Counter Tool, or write the FetchXml to count the records as you navigate from one page to the next. But, if you just want to use the SDK here is a simple method: a new message called RetrieveTotalRecordCountRequest. Here is how to use it in C#:
var r = new RetrieveTotalRecordCountRequest(); r.EntityNames = new[] { "contact", "account" }; var m = ((RetrieveTotalRecordCountResponse)this.Execute(r)).EntityRecordCountCollection; m.Dump();
Here is the same call in JavaScript:
var entities = ['contact','account']; fetch(`${Xrm.Utility.getGlobalContext().getClientUrl()}/api/data/v9.1/RetrieveTotalRecordCount(EntityNames=@EntityNames)?@EntityNames=${encodeURIComponent(JSON.stringify(entities))}`, { credentials: 'include', headers: { 'accept': 'application/json', 'content-type': 'application/json; charset=utf-8', 'odata-maxversion': '4.0', 'odata-version': '4.0', }, method: 'GET', mode: 'cors' }) .then(x => x.json()) .then(r => console.log(r.EntityRecordCountCollection.Keys.forEach((k, i) => console.log(`${k}: ${r.EntityRecordCountCollection.Values[i]}`))));
Hope this is useful.
Reference:
Reblogged this on Nishant Rana's Weblog and commented:
RetrieveTotalRecordCountRequest to get the total record counts instead of navigating to pages 🙂
Thanks Natraj for sharing this 🙂
[…] https://dreamingincrm.com/2019/07/22/getting-entity-record-counts/ […]
Nice feature!
Please don’t make new examples with Xrm.Page!
Xrm.Page.context is still supported.EDIT: Valid point. Updated the code to use Xrm.Utility.getGlobalContext().getClientUrl()
It still is, but it is marked as deprecated since version 9.0. That mean it is nearly 2 years and could be announced to be removed within the next 6 month. Don’t use it and please don’t promote it. Would love to see you to rewrite your example to use Xrm.Utility.getGlobalContext instead. I didn’t mean to criticize your contribution negatively, just to point out a possible improvement.
Meh now i was to slow to respond. Thanks for updating it tho ❤
[…] https://dreamingincrm.com/2019/07/22/getting-entity-record-counts/ […]
[…] https://dreamingincrm.com/2019/07/22/getting-entity-record-counts/ […]
[…] https://dreamingincrm.com/2019/07/22/getting-entity-record-counts/ […]
[…] Natraj shows you how to getting entity record counts in C# and JavaScript […]
[…] Natraj shows you how to getting entity record counts in C# and JavaScript […]
[…] Natraj shows you how to getting entity record counts in C# and JavaScript […]