How to get count of entities in Microsoft CRM 2011

Working on a task, I wanted to know exact number of records for particular entity in CRM 2011 but I wasn’t able to access this information via some standard way and LINQ query with .Count was throwing exception that this type of query is no implemented – The method ‘Count’ is not supported. (I’m talking about class that is generated by crmsvcutil.exe tool from CRM 2011 SDK).

How to solve this… hmmm.

So, there are several posibilites :

When accessing entity with small number of records, you can use this approach :

https://community.dynamics.com/product/crm/crmnontechnical/b/crmsoftwareblog/archive/2011/05/20/fetchxml-2011-total-record-count.aspx

but there is small caveat, default paging in CRM 2011 is set to 5000 records (at least on system that I have my hands on). So returned XML will claim that there is 5000 records, but you have to watch for EntityCollection.MoreRecords bool that is set when this is just page of entities also with PagingCookie that has the value of last record (XML fragment) that CRM knows where to continue (essentialy there is GUID with last record).

Or you can use my snippet :

I created small chunk of code that can retrieve total number of your desired entity and return this as int.

Introducing > the code 🙂 :

/// <summary>
/// Gets the supplied entity count from CRM 2011.
/// </summary>
/// <param name="Service">The CRM 2010 service.</param>
/// <param name="EntityName">Name of the entity we need get count of.</param>
/// <param name="ServicePageSize">Size of the page (optional).</param>
/// <returns></returns>
private int GetEntityCount(IOrganizationService Service, string EntityName, int ServicePageSize = 5000)
{
	RetrieveMultipleResponse retrieved;
	int PageNumber = 1;
	string PagingCookie = string.Empty;
	int PageSize = ServicePageSize;

	do {
		QueryExpression query = new QueryExpression() {
			EntityName = EntityName,
			//ColumnSet = new ColumnSet(Columns),
			PageInfo = new PagingInfo() {
				PageNumber = 1,
				Count = PageSize
			}
		};

		if (PageNumber != 1) {
			query.PageInfo.PageNumber = PageNumber;
			query.PageInfo.PagingCookie = PagingCookie;
		}

		RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
		retrieve.Query = query;
		retrieved = (RetrieveMultipleResponse)Service.Execute(retrieve);

		if (retrieved.EntityCollection.MoreRecords) {
			PageNumber++;
			PagingCookie = retrieved.EntityCollection.PagingCookie;
		}
	} while (retrieved.EntityCollection.MoreRecords);

	return ((PageNumber - 1) * PageSize) + retrieved.EntityCollection.Entities.Count;
}

IOrganizationService is class that is in Microsoft.Xrm.Sdk namespace and is used to connect to CRM. I’m using late binding here, so I created via tool (crmsvcutil.exe mentioned earlier) one class per every entity and thus I can call the method like this :

int acc = GetEntityCount(_serviceProxy, Account.EntityLogicalName);

Alternatively you could use string for the second parameter like this :

int acc = GetEntityCount(_serviceProxy, "account");

_serviceProxy is instance properly initialized with credentials and URL to running CRM 2011.

RetrieveMultipleRequest is class located in Microsoft.Crm.Sdk.Messages.

Optionally you could add filtering to the QueryExpression to apply some logic. There are several examples on other blogs.

The traffic from CRM 2011 server is as small as possible, because as you can see (I have commented out ColumnSet collection) there are no columns specified so only column that will be sent is ID (of type GUID).

If you have any additional questions or perhaps there is some easyer way, pls mail me or write them down in the comments.

CRMHelpers.zip – complete class, just include it in your project with referenced assemblies and you are good to go. Feel free to modify.

Hope this helps, enjoy 🙂

7 thoughts on “How to get count of entities in Microsoft CRM 2011

  1. Hi Thanks a lot for this and
    I need to get the count of the records from crm base on a picklist values,
    Ex: In Case entity there is a pick list called Status Code, now I need to get the count of cases of each of these pick type.

    Thanks in advance.
    viswan

Leave a Reply

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