public string ExportRecords(ExportConfiguration configuration) { try { log.Log(new LogEntry("Exporting records using provided configuration.", information: configuration.ToJson())); options = configuration.ExportOptions; log.Log($"Going over {configuration.Records.Count} record definitions in parallel ..."); Parallel.ForEach(configuration.Records, new ParallelOptions { MaxDegreeOfParallelism = CrmService.Threads }, recordsConfig => LoadEntities(recordsConfig)); log.Log($"Finished going over record definitions."); if (configuration.ExportOptions.IsExcludeOwner == true) { log.Log("Removing owner values from all records ..."); foreach (var record in recordsMap.Values .Select(e => e.Record) .Where(e => e.Attributes.Keys.Intersect(ownerFields).Any())) { var recordOwnerFields = record.Attributes.Keys.Intersect(ownerFields).ToArray(); foreach (var field in recordOwnerFields) { record.Attributes.Remove(field); } log.Log($"Removed '${recordOwnerFields.Aggregate((f1, f2) => $"{f1},{f2}")}'."); } log.Log("Finished removing owner values from all records."); } log.Log("Clearing formatted values from all records ..."); foreach (var formattedValues in recordsMap.Values .Select(e => e.Record.FormattedValues) .Where(f => f.Any())) { formattedValues.Clear(); } log.Log("Finished cleared formatted values from all records."); var exportedData = new ExportedData { Configuration = configuration, EntityDefinition = recordsMap, RelationDefinition = relationsMap, Queries = queries }; log.Log("Serialising exported data ..."); var serialisedData = configuration.ExportOptions.IsCompressData == true ? Helpers.Helpers.Compress(exportedData) : Encoding.UTF8.GetString(Helpers.Helpers.Serialise(exportedData)); log.Log("Finished serialising exported data."); log.Log($"Finished exporting records using provided configuration."); return(serialisedData); } catch (AggregateException exception) { throw new Exception(exception.InnerExceptions .Select(e => $"{e.GetType()} => {e.Message}" + $"{(e.InnerException == null ? "" : $"{e.InnerException.GetType()} => {e.InnerException.Message}")}") .Aggregate((e1, e2) => $"{e1} ||| {e2}")); } }