Sample code to retrieve more than 5000 records using FetchXML in CRM


Hi,

Sharing a sample code to retrieve more than 5000 records using the Fetch XML.

Version 1 :

</p>
<p>private List&lt;Entity&gt; GetTotalRecordsfromFetch(string fetchXML, IOrganizationService orgService)<br />
{<br />
List&lt;Entity&gt; lstEntity = new List&lt;Entity&gt;();</p>
<p>int fetchCount = 5000;<br />
int pageNumber = 1;<br />
string pagingCookie = null;</p>
<p>while (true)<br />
{<br />
// Build fetchXml string with the placeholders.<br />
string xml = this.CreateXml(fetchXML, pagingCookie, pageNumber, fetchCount);<br />
RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest<br />
{<br />
Query = new FetchExpression(xml)<br />
};</p>
<p>var returnCollections = ((RetrieveMultipleResponse)orgService.Execute(fetchRequest)).EntityCollection;</p>
<p>if (returnCollections.Entities.Count &gt;= 1)<br />
{<br />
lstEntity.AddRange(returnCollections.Entities);<br />
}</p>
<p>if (returnCollections.MoreRecords)<br />
{<br />
pageNumber++;</p>
<p>results.pagingCookie = returnCollections.PagingCookie;<br />
}<br />
else<br />
{<br />
// If no more records in the result nodes, exit the loop.<br />
break;<br />
}<br />
}</p>
<p>return lstEntity;<br />
}</p>
<p>public string CreateXml(string xml, string cookie, int page, int count)<br />
{<br />
StringReader stringReader = new StringReader(xml);<br />
XmlTextReader reader = new XmlTextReader(stringReader);</p>
<p>XmlDocument doc = new XmlDocument();<br />
doc.Load(reader);</p>
<p>XmlAttributeCollection attrs = doc.DocumentElement.Attributes;</p>
<p>if (cookie != null)<br />
{<br />
XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");<br />
pagingAttr.Value = cookie;<br />
attrs.Append(pagingAttr);<br />
}</p>
<p>XmlAttribute pageAttr = doc.CreateAttribute("page");<br />
pageAttr.Value = System.Convert.ToString(page);<br />
attrs.Append(pageAttr);</p>
<p>XmlAttribute countAttr = doc.CreateAttribute("count");<br />
countAttr.Value = System.Convert.ToString(count);<br />
attrs.Append(countAttr);</p>
<p>StringBuilder sb = new StringBuilder(1024);<br />
StringWriter stringWriter = new StringWriter(sb);</p>
<p>XmlTextWriter writer = new XmlTextWriter(stringWriter);<br />
doc.WriteTo(writer);<br />
writer.Close();</p>
<p>return sb.ToString();<br />
}</p>
<p>

Version 2 :

</p>
<p>private static List&lt;Entity&gt; GetTotalRecordsFetchXML(OrganizationServiceProxy orgProxy, string fetchXML)<br />
{<br />
XDocument xDocument = XDocument.Parse(fetchXML);<br />
var fetchXmlEntity = xDocument.Root.Element("entity").ToString();</p>
<p>EntityCollection entityColl = new EntityCollection();<br />
List&lt;Entity&gt; lstEntity = new List&lt;Entity&gt;();<br />
int page = 1;<br />
do<br />
{</p>
<p>entityColl = orgProxy.RetrieveMultiple(new FetchExpression(<br />
string.Format("&lt;fetch version='1.0' page='{1}' paging-cookie='{0}'&gt;" + fetchXmlEntity + "&lt;/fetch&gt;",<br />
SecurityElement.Escape(entityColl.PagingCookie), page++)));</p>
<p>lstEntity.AddRange(entityColl.Entities);<br />
}<br />
while (entityColl.MoreRecords);</p>
<p>return lstEntity;<br />
}</p>
<p>

Hope it helps..

Advertisements

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Sample code to retrieve more than 5000 records using FetchXML in CRM”

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.