Dynamics CRM SDK Nugget: TopCount

I still occasionally run into hidden greatness inside of the Dynamics CRM SDK.  Last week I found a great addition to the QueryExpression and QueryByAttribute classes: The TopCount property.

This property was introduced into Dynamics CRM 2011 around the UR10-UR11 timeframe, so if you are on UR11, or later, or have Dynamics CRM 2013, you have this capability.

TopCount works exactly like the TOP operator from a SQL SELECT statement:

SELECT TOP 50 * FROM FilteredAccount

 

Here is how you use it:

Note: I have a small function called IsTopCountSupportedByServer that checks the version number against UR11 to make sure we can use TopCount.  If not, we drop back to the standard PagingInfo class.

var maxBatchSize = 50;

QueryExpression queryExpression = new QueryExpression(Account.EntityLogicalName);

if (IsTopCountSupportedByServer())
{
    queryExpression.TopCount = new int?(maxBatchSize);
}
else
{
    queryExpression.PageInfo.Count = maxBatchSize;
    queryExpression.PageInfo.PageNumber = 1;
}

This is just a really cool addition to our toolboxes and I am shocked I didn’t see this before.

Leave a Reply 0 comments