Saturday, July 4, 2020

Retrieve 5000 records in Fetch Xml

In this post , we are going to see about how to retrieve the crm data  if it has more than 5000 records in the entity collection. Below code will help to sort the this issue , by using paging we can actually retrieve more than 5000 records.


Code:


int page = 1;
            List<Entity>caselist = new List<Entity>();
            EntityCollection casecollection= new EntityCollection();
 
            do
            {
                string fetchXml = @"<fetch distinct='true' mapping='logical' no-lock='true' page='{1}' paging-cookie='{0}' output-format='xml-platform' version='1.0'>
                                    <entity name='incident'>
                                        <attribute name='name'/>
                                        <attribute name='customer'/>
                                        <attribute name='casetitle'/>
                                        <attribute name='caseid'/>
                                        <attribute name='contact'/>
                                       
                                        <order descending='false' attribute='caseid'/>
                                        <filter type='and'>
                                            <condition attribute='statuscode' value='1'
operator='eq'/> 
<condition attribute='customer' value='{2}'/>                                          
                                        </filter>
                                          </entity>
                                </fetch>";
 
               
 
                casecollection= organizationService.RetrieveMultiple(new FetchExpression(string.Format(fetchXml, SecurityElement.Escape(casecollection.PagingCookie), page++, customerid)));
 
                if (casecollection!= null&& casecollection.Entities != null && casecollection.Entities.Count > 0)
                {
                    tracingService.Trace(" Retrieved: " + casecollection.Entities.Count);
                    caselist.AddRange(casecollection.Entities.ToList());
                }
                else
                    tracingService.Trace("Not fetched any record.");
            }
            while (casecollection.MoreRecords);
 
            if (caselist.Count() > 0)
            {
                return caselist;
            }
            else
            {
                return null;
            }
        }

No comments:

Post a Comment