Recent Posts

ads

How to retrive more than 5k Records through C# Console

Retrive more than 5k Records through C# Console

In this post I will tell the best way to retrive more than 5k records through  C# Console. Here I am utilizing question articulation and paging treat. In Microsoft CRM we have a few constraints is accessible so we couldn't retrive more records at a solitary snap. At that point we need to beat that difficult we use paging treat in question articulation. 

Snap here to see to retrive more than 5K records utilizing : javascript

C#





Retrive more than 5k Records through Console


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
namespace connectiontocrm
{
    class CRM
    {
        static void Main(string[] args)
        {

            IOrganizationService organizationService = null;

            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = "mohanreddy@1234lev.onmicrosoft.com";
                clientCredentials.UserName.Password = "Levtech@123";

                //clientcrentials



                // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
                // Copy and Paste Organization Service Endpoint Address URL

                OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(new Uri("https://1234lev.api.crm.dynamics.com/XRMServices/2011/Organization.svc"),
                 null, clientCredentials, null);
                IOrganizationService service;
                service = (IOrganizationService)serviceproxy;
                QueryExpression query = new QueryExpression()
                {
                    EntityName = "new_offeritem",
                    // ColumnSet = new ColumnSet() { Columns = { "new_muhammedmacintyre" } },
                    ColumnSet = new ColumnSet(false),
                    //Criteria=FIlter
                };
                //ParentQuery.Criteria.AddFilter(FIlter);

                query.PageInfo = new PagingInfo();
                query.PageInfo.Count = 5000;
                query.PageInfo.PageNumber = 1;
                query.PageInfo.ReturnTotalRecordCount = true;
                EntityCollection entityCollection = service.RetrieveMultiple(query);
                EntityCollection final = new EntityCollection();
                foreach (Entity i in entityCollection.Entities)
                {
                    final.Entities.Add(i);
                }
                do
                {
                    query.PageInfo.PageNumber += 1;
                    query.PageInfo.PagingCookie = entityCollection.PagingCookie;
                    entityCollection = service.RetrieveMultiple(query);
                    foreach (Entity i in entityCollection.Entities)
                        final.Entities.Add(i);
                }
                while (entityCollection.MoreRecords);
                int count = final.Entities.Count;
                // SampleData.Attributes["new_totalrecords"] = Convert.ToInt32(count);
                //Service.Update(SampleData);
                Console.WriteLine("totalcount :" + count);

            }

            catch (Exception ex)
            {
                Console.WriteLine("Exception caught - " + ex.Message);
            }
            Console.ReadKey();



        }

    }
}

Post a Comment

0 Comments