Retrive more than 5000 record using paging concept in fetchxml using c# MSCRM

 private List<Entity> GetDestinationRecords1()
        {
            List<Entity> AllRecords = new List<Entity>();
            try
            {
                int fetchCount2 = 5000;//
                int pageNumber2 = 1;

                string cookie2 = null;
                bool HasMoreRecords2 = true;
                //Guid id=new Guid("1BA1BCB9-20B6-E611-80FB-5065F38B2531");
                string fetchXml2 = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
  "<entity name='contact'>" +
    "<attribute name='telephone1' />" +
    "<attribute name='contactid' />" +
    "<attribute name='emailaddress1' />" +
    "<attribute name='lastname' />" +
    "<attribute name='firstname' />" +
    "<order attribute='telephone1' descending='false' />" +
    "<link-entity name='contact' from='contactid' to='parentcustomerid' visible='false' link-type='outer' alias='a_df70b5872164433aa551e577fcb1c644'>" +
      "<attribute name='new_titlelevel' />" +
    "</link-entity>" +
    "<link-entity name='account' from='accountid' to='parentcustomerid' alias='aa'>" +   
      "<attribute name='new_postmeetingnotes' />" +
      "<link-entity name='new_account_new_coredomain' from='accountid' to='accountid' visible='false' intersect='true'>" +
        "<link-entity name='new_coredomain' from='new_coredomainid' to='new_coredomainid' alias='ba'>" +
          "<filter type='and'>" +
            "<condition attribute='new_coredomain' operator='not-like' value='%Health%' />" +
          "</filter>" +
        "</link-entity>" +
      "</link-entity>" +
    "</link-entity>" +
  "</entity>" +
"</fetch>";

                EntityCollection ec2 = new EntityCollection();
                while (HasMoreRecords2)
                {
                    string fech2 = string.Format(fetchXml2, pageNumber2, SecurityElement.Escape(cookie2));
                    ec2 = service.RetrieveMultiple(new FetchExpression(fech2));
                    HasMoreRecords2 = ec2.MoreRecords;
                    foreach (Entity enty in ec2.Entities)
                        AllRecords.Add(enty);

                    if (HasMoreRecords2)
                    {
                        pageNumber2++;
                        cookie2 = ec2.PagingCookie;
                    }
                    else
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.Write("Exception " + ex.Message);
            }
            return AllRecords;
        }

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest