private static void MonitorJobProgress(IEnhancedOrgService service, Guid importJobId) { var progress = 0; ImportJob job = null; do { Thread.Sleep(5000); try { job = service.Retrieve("importjob", importJobId, new ColumnSet(ImportJob.Fields.Progress, ImportJob.Fields.CompletedOn)) .ToEntity <ImportJob>(); var currentProgress = (int?)job.Progress ?? 0; if (currentProgress - progress > 5) { log.Log($"... imported {progress = currentProgress}% ..."); } } catch { // ignored } }while (job?.CompletedOn == null); }
private void FillKeysForLookups(Entity record) { log.Log($"Filling alternate keys in lookups for {record} ..."); var keyedLookups = record.Attributes .Select(a => a.Value).OfType <EntityReference>() .Select(a => new { LookupRef = a, Keys = metadataHelper.GetAlternateKeys(a.LogicalName).ToArray() }) .Where(a => a.Keys.Any()) .ToArray(); Parallel.ForEach(keyedLookups, new ParallelOptions { MaxDegreeOfParallelism = CrmService.Threads }, keyedLookup => { var lookupRef = keyedLookup.LookupRef; var lookupKeys = keyedLookup.Keys; var id = $"'{lookupRef.LogicalName}':'{lookupRef.Id}'"; log.Log($"Retrieving lookup record {id} ..."); var lookupRecord = service.Retrieve(lookupRef.LogicalName, lookupRef.Id, new ColumnSet(lookupKeys)); log.Log($"Finished retrieving lookup record {id}."); lookupRef.KeyAttributes.AddRange(lookupKeys .ToDictionary(key => key, key => lookupRecord.GetAttributeValue <object>(key))); }); log.Log($"Finished filling alternate keys in lookups for {record}."); }
private static bool ImportSolution(ExportedSolution solution, IEnhancedOrgService service) { solution.Require(nameof(solution)); service.Require(nameof(service)); var importJobId = Guid.NewGuid(); var request = new ExecuteAsyncRequest { Request = new ImportSolutionRequest { CustomizationFile = solution.Data, ConvertToManaged = solution.IsManaged, OverwriteUnmanagedCustomizations = false, PublishWorkflows = true, SkipProductUpdateDependencies = true, ImportJobId = importJobId } }; log.Log($"Importing solution '{solution.SolutionName}' ..."); service.Execute(request); MonitorJobProgress(service, importJobId); var job = service.Retrieve("importjob", importJobId, new ColumnSet(ImportJob.Fields.Progress, ImportJob.Fields.Data)) .ToEntity <ImportJob>(); var importXmlLog = job.GetAttributeValue <string>("data"); if (importXmlLog.IsNotEmpty()) { var isFailed = ProcessErrorXml(importXmlLog); if (isFailed) { return(false); } } log.Log($"Imported!"); log.Log("Publishing customisations ..."); for (var i = 0; i < 3; i++) { Thread.Sleep(5000); try { service.Execute(new PublishAllXmlRequest()); log.Log("Finished publishing customisations."); break; } catch (Exception e) { log.Log(e); if (i < 2) { log.LogWarning("Retrying publish ..."); } } } return(true); }