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
Advertisements

FetchXML Formatter Online

With regards to my Post FetchXML Formatter Tool, Ashish created an online version of this Tool.

Click here and have a look at the FetchXML Formatter Online Version and it really helps as it can be accessed online by everyone from anywhere.

FetchXML Formatter Online Version

Really appreciate Ashish Vishwakarma’s effort in creating this Tool in Online Version.

 

FetchXML Formatter Tool

It is a Light weight windows application and this tool will be helpful when you are extensively working with FetchXML in Javascript / C# code to get desired result.

Functionality:

It will take FetchXML from Advance Find as an input and convert that into desired format, which can be used into Javascript / C# for further coding.

Pros:

  1. No Need to spend time for FetchXML formatting
  2. It is easy to use.
  3. Works for both Javascript and C# code

Example:

Let us take a simple example of the below FetchXML,

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

Provide this as an input to FetchXML formatter Tool, 1. Check Javascript radio button and click on Format to see the below output,

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

2. Check C# radio button and click on Format to see the below output,

@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='contact'>
    <attribute name='fullname' />
    <attribute name='telephone1' />
    <attribute name='emailaddress1' />
    <attribute name='contactid' />
    <order attribute='fullname' descending='false' />
    <filter type='and'>
      <condition attribute='ownerid' operator='eq-userid' />
      <condition attribute='statecode' operator='eq' value='0' />
    </filter>
  </entity>
</fetch>"

Click Here to download the FetchXML Formatter Tool Exe file

Screen Shots:

FetchXMLFormatter - Pic 1
FetchXMLFormatter - Pic 2
FetchXMLFormatter - Pic 3
FetchXMLFormatter - Pic 4
Please provide your valuable feedback on this article.

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