Tuesday, June 18, 2013

CRM 2011 FetchXml - Pagination and TotalCount (ReturnTotalRecordCount)

In order to provide pagination in a website that consumes information from CRM 2011, to know the total count of a fetchxml is a nice to have feature in order to provide this information to user.
In CRM 4 this must be done with two fetchxml operations, one to know the total and another one to do the actual fetchxml needed to provided the list of items to the user.
In CRM 2011 with the addition of new attribute returntotalrecordcount:
returntotalrecordcount='true'
We no longer need two fetchxml operations.

Here is a fetchxml example:
<fetch mapping='logical' count='50' returntotalrecordcount='true'>
   <entity name='contact'>
      <attribute name='contactid' />    
      <filter>
         <condition attribute='address1_city' operator='like' value='%Lisbon%' />
      </filter>
   </entity>
</fetch>

If you wanna do some tests, you can use following tool (actually this tool does more that just run fetchxml queries, but you only need the FetchXml Tester):
http://mscrmtools.blogspot.pt/2013/01/xrmtoolbox-updated-12012117.html

More links:
http://msdn.microsoft.com/en-us/library/gg309405.aspx
http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.paginginfo.returntotalrecordcount.aspx
http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.entitycollection.totalrecordcount.aspx
http://msdn.microsoft.com/en-us/library/gg334688.aspx