Skip to content

Getting Total Record Count in CRM 2011

Many a times I run into this particular situation and somehow I observe people still retrieve all records and then loop through to get the total number of records. It is very easy to get it using the code given below.

QueryExpression query = new QueryExpression("account");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.NotNull));
            query.PageInfo = new PagingInfo();
            query.PageInfo.Count = 1;
            query.PageInfo.PageNumber = 1;
            query.PageInfo.ReturnTotalRecordCount = true;

            EntityCollection result = this.ServiceProxy.RetrieveMultiple(query);
            int totalRecords = result.TotalRecordCount;

4 thoughts on “Getting Total Record Count in CRM 2011”

  1. What if there are more than 5000 records to retrieve.
    Then the ReturnTotalRecordCount will be 5000 and we still don't know how many records there are exactly.

    1. you can use fetchxml like this:
      string _recordcount = “” +
      “” +
      “” +
      “” +
      “” +
      “” +
      “” +
      “”;
      //RetrieveMultiple method
      EntityCollection resultColl = service.RetrieveMultiple(new FetchExpression(_recordcount));
      foreach (var c in resultColl.Entities)
      {
      int _recordCount = (Int32)((AliasedValue)c[“recordcount”]).Value;
      //_recordCount shows all of records
      }

Leave a Reply

Your email address will not be published. Required fields are marked *