For version 9.0 please check the below post
RetrieveTotalRecordCountRequest
https://dreamingincrm.com/2019/07/22/getting-entity-record-counts/
Just sharing a sample code to get the total number of records (e.g. account here)
IOrganizationService orgService = GetOrganizationService(); int totalCount = 0; QueryExpression query = new QueryExpression("account"); query.ColumnSet = new ColumnSet(); query.Distinct = true; query.ColumnSet.AddColumn("accountid"); query.PageInfo = new PagingInfo(); query.PageInfo.Count = 5000; query.PageInfo.PageNumber = 1; query.PageInfo.ReturnTotalRecordCount = true; EntityCollection entityCollection = orgService.RetrieveMultiple(query); totalCount = entityCollection.Entities.Count; while (entityCollection.MoreRecords) { query.PageInfo.PageNumber += 1; query.PageInfo.PagingCookie = entityCollection.PagingCookie; entityCollection = orgService.RetrieveMultiple(query); totalCount = totalCount + entityCollection.Entities.Count; } MessageBox.Show(totalCount.ToString());
Hope it helps
While this certainly works, it is not a very efficient solution. You are downloading and enumerating all accounts in the system when all you really want is the total count. Instead I would suggest you take a look at FetchXML aggregation: https://msdn.microsoft.com/en-us/library/gg309565.aspx#count
LikeLike
Thanks ! It would fail in case of more that 50000 records
System.ServiceModel.FaultException`1: ‘AggregateQueryRecordLimit exceeded. Cannot perform this operation.’
LikeLike
it’s work on account Entity
i change the QueryExpression to other Entity,
when i got the error “Paging cookie is required when trying to retrieve a set of records on any high pages”
LikeLike
i found the problem,the target field must include the primary key
LikeLike