Tuesday, November 12, 2019

Retrieve more than 5000 records using query expression


Retrive more than 5000 records using query expression.

In Dynamic CRM. fetchxml or query expression will give only 5000 records for retrievemultiple request. If entities has more than 5000 records while retrieving then in this case we should have use paging concept in query expression or in fetch xml. Below is the example to retrieve more than 5k records using query expression.

int pageNumber = 1;
string pagingCookie = null;

EntityCollection CaseRecord = new EntityCollection();
EntityCollection CaseList = new EntityCollection();


do
                {
                    QueryExpression CaseDetail = new QueryExpression
                    {
                        EntityName = "incident",
                        ColumnSet = new ColumnSet("casenumber""name""regardingobjectid"),
                        PageInfo = new PagingInfo()
                        {
                            Count = 5000,
                            PageNumber = pageNumber,
                            PagingCookie = pagingCookie
                        },
                    };

                    CaseDetail.Criteria.AddCondition("name"ConditionOperator.Equal"Test");
                    CaseDetail.Criteria.AddCondition("createdon"ConditionOperator.On"2019-10-19");
                    CaseDetail.Criteria.AddCondition("statuscode"ConditionOperator.Equal"32");

                    CaseRecord = organizationService.RetrieveMultiple(CaseDetail);

                   
                    CaseList.Entities.AddRange(CaseRecord.Entities);

                    pageNumber++;
                    pagingCookie = CaseRecord.PagingCookie;

                }
                while (CaseRecord.MoreRecords);


if (CaseList != null && CaseList.Entities != null && CaseList.Entities.Count > 0)
                {
                    foreach (Entity entity in CaseList.Entities)
                    {
                        int i = 1;
                        if (entity.Attributes.Contains("regardingobjectid") && entity.Attributes["regardingobjectid"] != null)
                        {

                            
                            Guid Regardingobjectid = (((EntityReference)entity.Attributes["regardingobjectid"]).Id);

 String name = entity.Attributes["name"].Tostring();

String casenumber = entity.Attributes["incidentnumber"].Tostring();


}
}
}

No comments:

Post a Comment