private static void LinqEarlyBound() { count = 0; CdsAuthHelper cdsAuthHelper = new CdsAuthHelper(); using (var proxy = new OrganizationWebProxyClient(cdsAuthHelper.serviceUrl, true)) { // Set Header with the token proxy.HeaderToken = cdsAuthHelper.token; // Get Crm Service Context (Early bound) CrmServiceContext context = new CrmServiceContext(proxy); // Retrieve Account records var accounts = (from m in context.AccountSet orderby m.AccountId select new { m.Name, m.AccountId } ); count = accounts.ToList().Count; } }
private static void LinqLateBound() { count = 0; CdsAuthHelper cdsAuthHelper = new CdsAuthHelper(); using (var proxy = new OrganizationWebProxyClient(cdsAuthHelper.serviceUrl, false)) { // Set Header with the token proxy.HeaderToken = cdsAuthHelper.token; // Get Organization Service Context (Late bound) OrganizationServiceContext context = new OrganizationServiceContext(proxy); // Retrieve Account records var accounts = (from m in context.CreateQuery("account") orderby m.GetAttributeValue <Guid>("accountid") select new { name = m.GetAttributeValue <string>("name"), accountid = m.GetAttributeValue <Guid>("accountid"), } ); count = accounts.ToList().Count; } }
private static void QueryExpression() { count = 0; CdsAuthHelper cdsAuthHelper = new CdsAuthHelper(); using (var proxy = new OrganizationWebProxyClient(cdsAuthHelper.serviceUrl, false)) { // Set Header with the token proxy.HeaderToken = cdsAuthHelper.token; // Get Organization Service IOrganizationService service = proxy; // Initialize the page number. int pageNumber = 1; //Create a column set. ColumnSet columns = new ColumnSet("name", "accountid"); // Create query expression. QueryExpression query = new QueryExpression(); query.ColumnSet = columns; query.EntityName = "account"; query.NoLock = true; // Assign the pageinfo properties to the query expression. query.PageInfo = new PagingInfo(); query.PageInfo.Count = recordsPerPage; query.PageInfo.PageNumber = pageNumber; query.NoLock = true; // The current paging cookie. When retrieving the first page, pagingCookie should be null. query.PageInfo.PagingCookie = null; while (true) { // Retrieve the page. EntityCollection results = service.RetrieveMultiple(query); // Check for more records, if it returns true. if (results.MoreRecords) { pageNumber++; // Increment the page number to retrieve the next page. query.PageInfo.PageNumber = pageNumber; // Set the paging cookie to the paging cookie returned from current results. query.PageInfo.PagingCookie = results.PagingCookie; } else { // If no more records are in the result nodes, exit the loop. count = (recordsPerPage * pageNumber) + results.Entities.Count; break; } } } }
private static void FetchXml() { count = 0; CdsAuthHelper cdsAuthHelper = new CdsAuthHelper(); using (var proxy = new OrganizationWebProxyClient(cdsAuthHelper.serviceUrl, false)) { // Set Header with the token proxy.HeaderToken = cdsAuthHelper.token; // Get Organization Service IOrganizationService service = proxy; string fetch = "<fetch mapping='logical' no-lock='true'>"; fetch += "<entity name='account'>"; fetch += "<attribute name='name'/>"; fetch += "<attribute name='accountid'/>"; fetch += "</entity>"; fetch += "</fetch>"; // Initialize the page number. int pageNumber = 1; // Specify the current paging cookie. For retrieving the first page, pagingCookie should be null. string pagingCookie = null; while (true) { // Build fetchXml string with the placeholders. string xml = CreateXml(fetch, pagingCookie, pageNumber, recordsPerPage); FetchExpression expression = new FetchExpression(xml); var results = service.RetrieveMultiple(expression); // Check for morerecords, if it returns 1. if (results.MoreRecords) { // Increment the page number to retrieve the next page. pageNumber++; pagingCookie = results.PagingCookie; } else { // If no more records in the result nodes, exit the loop. count = (recordsPerPage * pageNumber) + results.Entities.Count; break; } } } }
private static void FetchParallel() { count = 0; CdsAuthHelper cdsAuthHelper = new CdsAuthHelper(); using (var proxy = new OrganizationWebProxyClient(cdsAuthHelper.serviceUrl, false)) { // Set Header with the token proxy.HeaderToken = cdsAuthHelper.token; string fetch = "<fetch mapping='logical' no-lock='true'>"; fetch += "<entity name='account'>"; fetch += "<attribute name='name'/>"; fetch += "<attribute name='accountid'/>"; fetch += "</entity>"; fetch += "</fetch>"; IDictionary <string, QueryBase> queries = new Dictionary <string, QueryBase>(); queries.Add("result", new FetchExpression(fetch)); Microsoft.Xrm.Tooling.Connector.CrmServiceClient context = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(proxy); var manager = new OrganizationServiceManager(context); IDictionary <string, EntityCollection> results = null; try { results = manager.ParallelProxy.RetrieveMultiple(queries, true); foreach (var result in results) { count += result.Value.Entities.Count; } } catch (AggregateException ae) { // Handle exceptions } } }