private void ExportListOfInstancesForAllProjects()

in 2LCS/Forms/MainForm.cs [801:913]


        private void ExportListOfInstancesForAllProjects(LCSEnvironments _LCSEnvironments, LCSProjectAllCurrent _LCSProjectAllCurrent)
        {
            notifyIcon.BalloonTipText = $"Exporting list of {_LCSEnvironments} instances for {_LCSProjectAllCurrent} LCS projects. Please wait...";
            notifyIcon.BalloonTipTitle = $"Exporting list of {_LCSEnvironments} instances";

            notifyIcon.ShowBalloonTip(2000); //This setting might be overruled by the OS

            Cursor = Cursors.WaitCursor;
            var previousProject = _selectedProject;
            var exportedInstances = new List<ExportedInstance>();

            if (_LCSProjectAllCurrent == LCSProjectAllCurrent.ALL)
            {
                Projects = _httpClientHelper.GetAllProjects();
            }
            else if (_LCSProjectAllCurrent == LCSProjectAllCurrent.CURRENT)
            {
                Projects = new List<LcsProject>();
                Projects.Add(previousProject);
            }

            Projects = ExcludeProjectsForOrganization(Projects); //remove all internal projects for export.

            foreach (var _project in Projects)
            {
                if (_project.RequestPending == true) continue;
                _selectedProject = _project;
                _httpClientHelper.ChangeLcsProjectId(_project.Id.ToString());
                _httpClientHelper.LcsProjectTypeId = _project.ProjectTypeId;
                SetLcsProjectText();
                RefreshChe();
                RefreshSaas();

                if (_LCSEnvironments == LCSEnvironments.ALL || _LCSEnvironments == LCSEnvironments.SAAS)
                    if (_saasInstancesList != null && _saasInstancesList.Count > 0)
                    {
                        foreach (var _instance in _saasInstancesList)
                        {
                            var exportedInstance = new ExportedInstance
                            {
                                ProjectId = _project.Id.ToString(),
                                ProjectName = _project.Name,
                                Organization = _project.OrganizationName,
                                InstanceName = _instance.DisplayName,
                                EnvironmentId = _instance.EnvironmentId,
                                CurrentApplicationBuildVersion = _instance.CurrentApplicationBuildVersion,
                                CurrentApplicationReleaseName = _instance.CurrentApplicationReleaseName,
                                CurrentPlatformReleaseName = _instance.CurrentPlatformReleaseName,
                                CurrentPlatformVersion = _instance.CurrentPlatformVersion,
                                BuildInfo = _instance.BuildInfo,
                                TopologyType = _instance.TopologyType,
                                TopologyVersion = _instance.TopologyVersion,
                                TopologyName = _instance.TopologyName,
                                DeploymentStatus = _instance.DeploymentStatus,
                                HostingType = "MS hosted"
                            };
                            exportedInstances.Add(exportedInstance);
                        }
                    }

                if (_LCSEnvironments == LCSEnvironments.ALL || _LCSEnvironments == LCSEnvironments.CHE)
                    if (_cheInstancesList != null && _cheInstancesList.Count > 0)
                    {
                        foreach (var _instance in _cheInstancesList)
                        {
                            var exportedInstance = new ExportedInstance
                            {
                                ProjectId = _project.Id.ToString(),
                                ProjectName = _project.Name,
                                Organization = _project.OrganizationName,
                                InstanceName = _instance.DisplayName,
                                EnvironmentId = _instance.EnvironmentId,
                                CurrentApplicationBuildVersion = _instance.CurrentApplicationBuildVersion,
                                CurrentApplicationReleaseName = _instance.CurrentApplicationReleaseName,
                                CurrentPlatformReleaseName = _instance.CurrentPlatformReleaseName,
                                CurrentPlatformVersion = _instance.CurrentPlatformVersion,
                                BuildInfo = _instance.BuildInfo,
                                TopologyType = _instance.TopologyType,
                                TopologyVersion = _instance.TopologyVersion,
                                TopologyName = _instance.TopologyName,
                                DeploymentStatus = _instance.DeploymentStatus,
                                HostingType = "Cloud hosted"
                            };
                            exportedInstances.Add(exportedInstance);
                        }
                    }
            }
            SaveFileDialog savefile = new SaveFileDialog
            {
                FileName = $"D365FO {_LCSEnvironments} instances - 2LCS generated.csv",
                Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
            };

            if (savefile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using StreamWriter sw = new StreamWriter(savefile.FileName, false, Encoding.Unicode);
                    var csv = new CsvWriter(sw, CultureInfo.CurrentCulture);
                    csv.WriteRecords(exportedInstances);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            _selectedProject = previousProject;
            _httpClientHelper.ChangeLcsProjectId(_selectedProject.Id.ToString());
            _httpClientHelper.LcsProjectTypeId = _selectedProject.ProjectTypeId;
            SetLcsProjectText();
            RefreshChe(false);
            RefreshSaas(false);
        }