Esempio n. 1
0
        private static bool IsSolutionUpdated(string solutionSolutionName, string solutionVersion,
                                              IEnhancedOrgService service)
        {
            solutionSolutionName.RequireNotEmpty(nameof(solutionSolutionName));
            solutionVersion.RequireNotEmpty(nameof(solutionVersion));
            service.Require(nameof(service));

            var versionString = RetrieveSolutionVersion(solutionSolutionName, service);

            if (string.IsNullOrWhiteSpace(versionString))
            {
                return(true);
            }

            var existingVersion = new Version(versionString);
            var givenVersion    = new Version(solutionVersion);
            var isUpdated       = givenVersion > existingVersion;

            if (isUpdated)
            {
                log.Log("Solution updated!");
            }

            return(isUpdated);
        }
Esempio n. 2
0
        private static string RetrieveSolutionVersion(string solutionName, IEnhancedOrgService service)
        {
            solutionName.RequireNotEmpty(nameof(solutionName));
            service.Require(nameof(service));

            var query =
                new QueryExpression
            {
                EntityName = Solution.EntityLogicalName,
                ColumnSet  = new ColumnSet(Solution.Fields.Version),
                Criteria   = new FilterExpression()
            };

            query.Criteria.AddCondition(Solution.Fields.Name, ConditionOperator.Equal, solutionName);

            log.Log($"Retrieving solution version for solution '{solutionName}'...");
            var solution = service.RetrieveMultiple(query).Entities.FirstOrDefault()?.ToEntity <Solution>();

            log.Log($"Version: {solution?.Version}.");

            return(solution?.Version);
        }
Esempio n. 3
0
        private static ExportedSolution RetrieveSolution(SolutionConfig solutionConfig,
                                                         IEnhancedOrgService service)
        {
            solutionConfig.Require(nameof(solutionConfig));
            service.Require(nameof(service));

            var version = RetrieveSolutionVersion(solutionConfig.SolutionName, service);

            if (string.IsNullOrWhiteSpace(version))
            {
                throw new NotFoundException($"Couldn't retrieve solution version of solution"
                                            + $" '{solutionConfig.SolutionName}'.");
            }

            var request =
                new ExportSolutionRequest
            {
                Managed      = solutionConfig.IsManaged,
                SolutionName = solutionConfig.SolutionName
            };

            log.Log($"Exporting solution '{solutionConfig.SolutionName}'...");
            var response = (ExportSolutionResponse)service.Execute(request);

            log.Log($"Exported!");

            var exportXml = response.ExportSolutionFile;

            return(new ExportedSolution
            {
                SolutionName = solutionConfig.SolutionName,
                Version = version,
                IsManaged = solutionConfig.IsManaged,
                Data = exportXml
            });
        }
Esempio n. 4
0
        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);
        }