Exemplo n.º 1
0
        public string ExportSolution(
            string outputFolder,
            SolutionExportOptions options)
        {
            Logger.LogVerbose("Exporting Solution: {0}", options.SolutionName);

            var      solutionFile = new StringBuilder();
            Solution solution     = GetSolution(options.SolutionName,
                                                new ColumnSet("version"));

            solutionFile.Append(options.SolutionName);

            if (options.IncludeVersionInName)
            {
                solutionFile.Append("_");
                solutionFile.Append(solution.Version.Replace(".", "_"));
            }

            if (options.Managed)
            {
                solutionFile.Append("_managed");
            }

            solutionFile.Append(".zip");

            var exportSolutionRequest = new ExportSolutionRequest
            {
                Managed      = options.Managed,
                SolutionName = options.SolutionName,
                ExportAutoNumberingSettings          = options.ExportAutoNumberingSettings,
                ExportCalendarSettings               = options.ExportCalendarSettings,
                ExportCustomizationSettings          = options.ExportCustomizationSettings,
                ExportEmailTrackingSettings          = options.ExportEmailTrackingSettings,
                ExportGeneralSettings                = options.ExportGeneralSettings,
                ExportIsvConfig                      = options.ExportIsvConfig,
                ExportMarketingSettings              = options.ExportMarketingSettings,
                ExportOutlookSynchronizationSettings = options.ExportOutlookSynchronizationSettings,
                ExportRelationshipRoles              = options.ExportRelationshipRoles,
                ExportSales   = options.ExportSales,
                TargetVersion = options.TargetVersion,
            };

            //keep seperate to allow compatibility with crm2015
            if (options.ExportExternalApplications)
            {
                exportSolutionRequest.ExportExternalApplications = options.ExportExternalApplications;
            }

            var exportSolutionResponse = OrganizationService.Execute(exportSolutionRequest) as ExportSolutionResponse;

            string solutionFilePath = Path.Combine(outputFolder, solutionFile.ToString());

            File.WriteAllBytes(solutionFilePath, exportSolutionResponse.ExportSolutionFile);

            Logger.LogInformation("Solution Zip Size: {0}", FileUtilities.GetFileSize(solutionFilePath));

            return(solutionFilePath);
        }
Exemplo n.º 2
0
        public string ExportSolution(
            string outputFolder,
            SolutionExportOptions options)
        {
            Logger.LogVerbose("Exporting Solution: {0}", options.SolutionName);

            var      solutionFile = new StringBuilder();
            Solution solution     = GetSolution(options.SolutionName,
                                                new ColumnSet("version"));

            if (solution is null)
            {
                throw new Exception($"Unable to find solution with unique name: {options.SolutionName}");
            }
            else
            {
                Logger.LogInformation($"Exporting Solution: {options.SolutionName}, version: {solution.Version}");
            }

            solutionFile.Append(options.SolutionName);

            if (options.IncludeVersionInName)
            {
                solutionFile.Append("_");
                solutionFile.Append(solution.Version.Replace(".", "_"));
            }

            if (options.Managed)
            {
                solutionFile.Append("_managed");
            }

            solutionFile.Append(".zip");

            var exportSolutionRequest = new ExportSolutionRequest
            {
                Managed      = options.Managed,
                SolutionName = options.SolutionName,
                ExportAutoNumberingSettings          = options.ExportAutoNumberingSettings,
                ExportCalendarSettings               = options.ExportCalendarSettings,
                ExportCustomizationSettings          = options.ExportCustomizationSettings,
                ExportEmailTrackingSettings          = options.ExportEmailTrackingSettings,
                ExportGeneralSettings                = options.ExportGeneralSettings,
                ExportIsvConfig                      = options.ExportIsvConfig,
                ExportMarketingSettings              = options.ExportMarketingSettings,
                ExportOutlookSynchronizationSettings = options.ExportOutlookSynchronizationSettings,
                ExportRelationshipRoles              = options.ExportRelationshipRoles,
                ExportSales   = options.ExportSales,
                TargetVersion = options.TargetVersion,
                RequestId     = Guid.NewGuid()
            };

            Logger.LogVerbose($"RequestId: {exportSolutionRequest.RequestId}");

            //keep seperate to allow compatibility with crm2015
            if (options.ExportExternalApplications)
            {
                exportSolutionRequest.ExportExternalApplications = options.ExportExternalApplications;
            }

            byte[] solutionBytes;

            if (options.ExportAsync)
            {
                Logger.LogInformation("Exporting Solution using Async Mode");

                exportSolutionRequest.RequestName = "ExportSolutionAsync";

                var asyncExportResponse = OrganizationService.Execute(exportSolutionRequest);

                //Guid asyncJobId = asyncResponse.AsyncJobId;
                Guid asyncJobId  = (Guid)asyncExportResponse.Results["AsyncOperationId"];
                Guid exportJobId = (Guid)asyncExportResponse.Results["ExportJobId"];

                Logger.LogInformation($"AsyncOperationId: {asyncJobId}");
                Logger.LogInformation($"ExportJobId: {exportJobId}");

                AsyncOperationManager asyncOperationManager = new AsyncOperationManager(Logger, OrganizationService);
                AsyncOperation        operation             = asyncOperationManager.AwaitCompletion(asyncJobId, options.AsyncWaitTimeout, options.SleepInterval, null);

                Logger.LogInformation("Async Operation completed with status: {0}",
                                      ((AsyncOperation_StatusCode)operation.StatusCode.Value).ToString());

                Logger.LogInformation("Async Operation completed with message: {0}",
                                      operation.Message);

                if (operation.StatusCode.Value == (int)AsyncOperation_StatusCode.Succeeded)
                {
                    OrganizationRequest downloadReq = new OrganizationRequest("DownloadSolutionExportData");
                    downloadReq.Parameters.Add("ExportJobId", exportJobId);

                    OrganizationResponse downloadRes = OrganizationService.Execute(downloadReq);

                    solutionBytes = (byte[])downloadRes.Results["ExportSolutionFile"];
                }
                else
                {
                    throw new Exception($"Export of solution '{options.SolutionName}' failed: {operation.Message}");
                }
            }
            else
            {
                Logger.LogInformation("Exporting Solution using Sync Mode");

                var exportSolutionResponse = OrganizationService.Execute(exportSolutionRequest) as ExportSolutionResponse;

                solutionBytes = exportSolutionResponse.ExportSolutionFile;
            }

            string solutionFilePath = Path.Combine(outputFolder, solutionFile.ToString());

            File.WriteAllBytes(solutionFilePath, solutionBytes);

            Logger.LogInformation($"Solution Exported to: {solutionFilePath}");
            Logger.LogInformation("Solution Zip Size: {0}", FileUtilities.GetFileSize(solutionFilePath));

            return(solutionFilePath);
        }