Retrieve 5000+ records using FetchXML and Query Expression

In my previous post, explained how to Retrieve 5000+ entity records using Query Expression,

In this post, will explain how to Retrieve 5000+ entity records using FetchXML and Query Expression.

Example: Retrieve All contact records where MobilePhone data is not null.

use below method,

public static EntityCollection RetrieveAllRecordsUsingFetchXML(IOrganizationService service, string fetchXML)
 {
 var moreRecords = false;
 int page = 1;
 var cookie = string.Empty;
 var entityCollection = new EntityCollection();
 do
 {
 var xml = string.Format(fetchXML, cookie);
 var collection = service.RetrieveMultiple(new FetchExpression(xml));

if (collection.Entities.Count > 0) entityCollection.Entities.AddRange(collection.Entities);

moreRecords = collection.MoreRecords;
 if (moreRecords)
 {
 page++;
 cookie = string.Format("paging-cookie='{0}' page='{1}'", System.Security.SecurityElement.Escape(collection.PagingCookie), page);
 }
 Console.WriteLine(entityCollection.Entities.Count);
 } while (moreRecords);

return entityCollection;
 }

Get the required FetchXML from CRM Advanced Find and format using FetchXML Formatter Online.

For the example stated above, required FetchXML is as follows,

string fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' {0}>" +
" <entity name='contact'>" +
" <attribute name='fullname' />" +
" <attribute name='mobilephone' />" +
" <attribute name='contactid' />" +
" <order attribute='fullname' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='mobilephone' operator='not-null' />" +
" </filter>" +
" </entity>" +
"</fetch>";

Note: Do not forget to append {0} in the first line of the formatted FetchXML.

How to Call Method?

EntityCollection contactCollection = RetrieveAllRecordsUsingFetchXML(_orgService, fetchXML);
Inspired from MS Dynamics CRM – Tips from a Developer Blog
Advertisement

Retrieve records using Fetch XML & Java Script in CRM 2011/13

Task: Retrieve Arun Potti Business Phone on onload of Contact record in Contact Entity using FetchXML & Javascript

Solution:

Step 1: Goto http://xrmsvctoolkit.codeplex.com/, and download the Zip folder. Unzip XrmServiceToolkit the folder.

Step 2: Open the XrmServiceToolkit folder, you can find the below Javascript files.

FetchXML - Pic 1

Goto Microsoft Dynamics CRM –> Settings –> Customization –> Webresources.

Create jquery, json2 and XrmServiceToolkit javascript webresources. While creating web resources browse for the respective files and provide the path of XRMServiceToolkit.

Step 3: Add all 3 files to the contact entity,

FetchXML - Pic 2

Step 4: Goto Microsoft Dynamics CRM –> Sales –> Contacts, click on Advance find button. Create new criteria as shown below by clicking on New button,

FetchXML - Pic 3

Step 5: Click on Download Fetch XML button, to get FetchXML code. You can see the below XML,

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="telephone1" />
    <attribute name="contactid" />
    <order attribute="fullname" descending="false" />
    <filter type="and">
     <condition attribute="fullname" operator="eq" value="Arun Potti" />
    </filter>
  </entity>
</fetch>

Step 6: We have to change the format of the above FetchXML to use in Javascript.

Use FetchXML Formatter Tool to modify the format.

To Know more about FetchXML Formatter Tool click Here

After modifying its looks like the below,

"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"  <entity name='contact'>"+
"    <attribute name='fullname' />"+
"    <attribute name='telephone1' />"+
"    <attribute name='contactid' />"+
"    <order attribute='fullname' descending='false' />"+
"    <filter type='and'>"+
"      <condition attribute='fullname' operator='eq' value='Arun Potti' />"+
"    </filter>"+
"  </entity>"+
"</fetch>"

Step 7: Now create new_contactJscript Webresource and paste the below code,

function contactOnload() {
 var contactFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
 "  <entity name='contact'>"+
 "    <attribute name='fullname' />"+
 "    <attribute name='telephone1' />"+
 "    <attribute name='contactid' />"+
 "    <order attribute='fullname' descending='false' />"+
 "    <filter type='and'>"+
 "      <condition attribute='fullname' operator='eq' value='Arun Potti' />"+
 "    </filter>"+
 "  </entity>"+
 "</fetch>";
var contactRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML);
if (contactRecords.length > 0) {
 if (contactRecords[0].attributes.telephone1 != undefined)
   alert(contactRecords[0].attributes.telephone1.value);
  }
}

Step 8: Add the new_contactJscript Webresource to Form Libraries on contact Form, and add the contactOnload function to Onload Event,

FetchXML - Pic 5

Step 9: Click on Ok. Save & Publish the contact Entity.

Step 10: Open the existing contact record to see the below pop up,

FetchXML - Pic 6