namespace <%= client %>.<%= package %>.Repositories { using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// /// Extensions to . /// public static class OrganizationServiceExtensions { /// /// Create a new . /// /// The type of . /// Organization service. /// A new context. public static TCrmContext CreateNewCrmContext(this IOrganizationService orgService) where TCrmContext : OrganizationServiceContext { var context = (TCrmContext)Activator.CreateInstance(typeof(TCrmContext), orgService); context.MergeOption = MergeOption.NoTracking; context.SaveChangesDefaultOptions = SaveChangesOptions.None; return context; } /// /// Retrive multiple records by column value. /// /// Organization service. /// The entity name. /// The column name. /// The value to filter on. /// The columns to select. /// The number of records per page. /// A collection of records meeting the criteria. public static EntityCollection GetEntitiesByColumn(this IOrganizationService orgService, string entityName, string columnName, object columnValue, string[] columnsToRetrieve = null, int pageSize = 100) { var query = new QueryExpression(entityName) { ColumnSet = columnsToRetrieve != null ? new ColumnSet(columnsToRetrieve) : new ColumnSet(true), }; if (!string.IsNullOrWhiteSpace(columnName) && columnValue != null) { query.Criteria.AddCondition(columnName, ConditionOperator.Equal, columnValue); } else if (!string.IsNullOrWhiteSpace(columnName) && columnValue == null) { query.Criteria.AddCondition(columnName, ConditionOperator.Null); } return orgService.GetDataByQuery(query, pageSize); } /// /// Get data by query expression. /// /// Organization service. /// The query expression. /// The number of records per page. /// Whether or not to include the entities in the response. /// An entity collection of records matching the query. public static EntityCollection GetDataByQuery(this IOrganizationService orgService, QueryExpression query, int pageSize, bool shouldIncudeEntityCollection = true) { var allResults = new EntityCollection(); query.PageInfo = new PagingInfo { Count = pageSize, PageNumber = 1, PagingCookie = null, }; while (true) { var pagedResults = orgService.RetrieveMultiple(query); if (shouldIncudeEntityCollection) { if (query.PageInfo.PageNumber == 1) { allResults = pagedResults; } else { allResults.Entities.AddRange(pagedResults.Entities); } } else { allResults.TotalRecordCount = allResults.TotalRecordCount + pagedResults.Entities.Count; } if (pagedResults.MoreRecords) { query.PageInfo.PageNumber++; query.PageInfo.PagingCookie = pagedResults.PagingCookie; } else { break; } } return allResults; } /// /// Execute multiple requests. /// /// Organization service. /// The requests to execute. /// The responses. public static ExecuteMultipleResponse ExecuteMultiple(this IOrganizationService orgService, OrganizationRequestCollection requests) { var requestWithResults = new ExecuteMultipleRequest { Settings = new ExecuteMultipleSettings { ContinueOnError = true, ReturnResponses = true, }, Requests = new OrganizationRequestCollection(), }; requestWithResults.Requests = requests; var responseWithResults = (ExecuteMultipleResponse)orgService.Execute(requestWithResults); return responseWithResults; } } }