private void ExportProjectDataToolStripMenuItem_Click()

in 2LCS/Forms/MainForm.cs [915:1297]


        private void ExportProjectDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshMenuItem_Click(null, null);
            var projectUsers = _httpClientHelper.GetAllProjectUsers();

            notifyIcon.BalloonTipText = $"Exporting data for {_selectedProject.Name} project. Please wait...";
            notifyIcon.BalloonTipTitle = "Exporting LCS project data";

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

            Cursor = Cursors.WaitCursor;

            using (var document = DocX.Create(_selectedProject.Name + " - 2LCS generated.docx"))
            {
                document.InsertParagraph(_selectedProject.Name).CapsStyle(CapsStyle.caps).FontSize(40d).SpacingBefore(50d).SpacingAfter(20d);
                document.InsertParagraph("Project Id: " + _selectedProject.Id).FontSize(14d).SpacingAfter(100d);
                document.InsertParagraph(_selectedProject.OrganizationName).CapsStyle(CapsStyle.caps).FontSize(30d).InsertPageBreakAfterSelf();

                Hyperlink link2LCS = document.AddHyperlink("2LCS", new Uri("https://github.com/Microsoft/2LCS"));
                document.AddFooters();
                document.AddHeaders();
                document.DifferentFirstPage = true;
                document.Footers.Odd.InsertParagraph("Generated with ").AppendHyperlink(link2LCS);

                var tocSwitches = new Dictionary<TableOfContentsSwitches, string>()
                {
                    { TableOfContentsSwitches.O, "1-3"},
                    { TableOfContentsSwitches.U, ""},
                    { TableOfContentsSwitches.Z, ""},
                    { TableOfContentsSwitches.H, ""},
                    { TableOfContentsSwitches.W, ""},
                    { TableOfContentsSwitches.X, ""},
                };

                var toc = document.InsertTableOfContents("Table of Contents", tocSwitches);

                var tocParagraph = document.InsertParagraph();
                tocParagraph.InsertPageBreakAfterSelf();

                //Subscriptions info
                var plans = _httpClientHelper.RetrieveTenantPlans();
                if (plans != null)
                {
                    var subsHeader = document.InsertParagraph("Subscriptions available").Heading(HeadingType.Heading1).FontSize(20d);
                    subsHeader.SpacingAfter(40d);
                    var tenantNameHeader = document.InsertParagraph("Tenant name: " + plans.TenantName).FontSize(14d);
                    tenantNameHeader.SpacingAfter(10d);
                    var tenantIdHeader = document.InsertParagraph("Tenant ID: " + plans.TenantId).FontSize(14d);
                    tenantIdHeader.SpacingAfter(10d);
                    if (plans.Plans.Count > 0)
                    {
                        //plans table
                        var plansColumnWidths = new float[] { 200f, 200f, 60f, 40f, 40f };
                        var plansDetailsTable = document.AddTable(1, plansColumnWidths.Length);
                        plansDetailsTable.SetWidths(plansColumnWidths);
                        plansDetailsTable.Design = TableDesign.LightListAccent6;
                        plansDetailsTable.Alignment = Alignment.left;
                        plansDetailsTable.AutoFit = AutoFit.Contents;
                        //headers
                        plansDetailsTable.Rows[0].Cells[0].Paragraphs[0].Append("Service plan name");
                        plansDetailsTable.Rows[0].Cells[1].Paragraphs[0].Append("Service plan ID");
                        plansDetailsTable.Rows[0].Cells[2].Paragraphs[0].Append("Assigned date");
                        plansDetailsTable.Rows[0].Cells[3].Paragraphs[0].Append("Quantity");
                        plansDetailsTable.Rows[0].Cells[4].Paragraphs[0].Append("Status");

                        foreach (var plan in plans.Plans)
                        {
                            var row = plansDetailsTable.InsertRow();
                            row.Cells[0].Paragraphs[0].Append(plan.ServicePlanName);
                            row.Cells[1].Paragraphs[0].Append(plan.ServicePlanId);
                            row.Cells[2].Paragraphs[0].Append(plan.DisplayAssignedDate);
                            row.Cells[3].Paragraphs[0].Append(plan.PrepaidUnitsEnabled.ToString());
                            row.Cells[4].Paragraphs[0].Append(plan.PlanStatus);
                        }
                        tenantIdHeader.InsertTableAfterSelf(plansDetailsTable);
                        document.InsertParagraph().InsertPageBreakAfterSelf();
                    }
                }

                if (projectUsers != null && projectUsers.Count > 0)
                {
                    var projectUsersHeader = document.InsertParagraph("Project users").Heading(HeadingType.Heading1).FontSize(20d);
                    projectUsersHeader.SpacingAfter(40d);
                    //users table
                    var usersColumnWidths = new float[] { 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f };
                    var usersDetailsTable = document.AddTable(1, usersColumnWidths.Length);
                    usersDetailsTable.SetWidths(usersColumnWidths);
                    usersDetailsTable.Design = TableDesign.LightListAccent3;
                    usersDetailsTable.Alignment = Alignment.left;
                    usersDetailsTable.AutoFit = AutoFit.Contents;
                    //headers
                    usersDetailsTable.Rows[0].Cells[0].Paragraphs[0].Append("Name");
                    usersDetailsTable.Rows[0].Cells[1].Paragraphs[0].Append("Email");
                    usersDetailsTable.Rows[0].Cells[2].Paragraphs[0].Append("Organization");
                    usersDetailsTable.Rows[0].Cells[3].Paragraphs[0].Append("Project role");
                    usersDetailsTable.Rows[0].Cells[4].Paragraphs[0].Append("User role");
                    usersDetailsTable.Rows[0].Cells[5].Paragraphs[0].Append("Allow service provider to contact");
                    usersDetailsTable.Rows[0].Cells[6].Paragraphs[0].Append("Added by");
                    usersDetailsTable.Rows[0].Cells[7].Paragraphs[0].Append("Added by organization");
                    usersDetailsTable.Rows[0].Cells[8].Paragraphs[0].Append("Created date");
                    usersDetailsTable.Rows[0].Cells[9].Paragraphs[0].Append("Status");

                    foreach (var user in projectUsers)
                    {
                        var row = usersDetailsTable.InsertRow();
                        if (user.UserProfile != null)
                        {
                            row.Cells[0].Paragraphs[0].Append(user.UserProfile.DisplayName);
                            row.Cells[1].Paragraphs[0].Append(user.UserProfile.Email);
                            row.Cells[2].Paragraphs[0].Append(user.UserProfile.Organization.Name);
                        }
                        else
                        {
                            row.Cells[1].Paragraphs[0].Append(user.InvitationEmail);
                        }
                        row.Cells[3].Paragraphs[0].Append(user.UserRoleDisplayText);
                        row.Cells[4].Paragraphs[0].Append(user.FunctionalRoleDisplayText);
                        row.Cells[5].Paragraphs[0].Append(user.AllowContactByMicrosoft.ToString());
                        row.Cells[6].Paragraphs[0].Append(user.InvitedBy.DisplayName);
                        row.Cells[7].Paragraphs[0].Append(user.InvitedBy.Organization.Name);
                        row.Cells[8].Paragraphs[0].Append(user.CreatedDate); //Todo ?.ToString("yyyy-MM-dd H:mm"));
                        row.Cells[9].Paragraphs[0].Append(user.InvitationStatusDisplayText);
                    }
                    projectUsersHeader.InsertTableAfterSelf(usersDetailsTable);
                    document.InsertParagraph().InsertPageBreakAfterSelf();
                }
                if (_saasInstancesList != null && _saasInstancesList.Count > 0)
                {
                    var instancesHeader = document.InsertParagraph("Microsoft managed instances").Heading(HeadingType.Heading1).FontSize(20d);
                    instancesHeader.SpacingAfter(40d);

                    foreach (var saasInstance in _saasInstancesList)
                    {
                        //instance header
                        var instanceHeader = document.InsertParagraph(saasInstance.DisplayName).CapsStyle(CapsStyle.caps).Heading(HeadingType.Heading2).FontSize(16d);
                        instanceHeader.SpacingAfter(10d);

                        //instance table
                        var instanceColumnWidths = new float[] { 200f, 400f };
                        var instanceDetailsTable = document.AddTable(11, instanceColumnWidths.Length);
                        instanceDetailsTable.SetWidths(instanceColumnWidths);
                        instanceDetailsTable.Design = TableDesign.LightListAccent2;
                        instanceDetailsTable.Alignment = Alignment.left;
                        instanceDetailsTable.Rows[0].Cells[0].Paragraphs[0].Append("Name");
                        instanceDetailsTable.Rows[0].Cells[1].Paragraphs[0].Append(saasInstance.DisplayName);
                        instanceDetailsTable.Rows[1].Cells[0].Paragraphs[0].Append("Environment Id");
                        instanceDetailsTable.Rows[1].Cells[1].Paragraphs[0].Append(saasInstance.EnvironmentId);
                        instanceDetailsTable.Rows[2].Cells[0].Paragraphs[0].Append("Topology");
                        instanceDetailsTable.Rows[2].Cells[1].Paragraphs[0].Append(saasInstance.TopologyDisplayName);
                        instanceDetailsTable.Rows[3].Cells[0].Paragraphs[0].Append("Deployed on");
                        instanceDetailsTable.Rows[3].Cells[1].Paragraphs[0].Append(saasInstance.DeployedOn); //Todo ?.ToString("yyyy-MM-dd H:mm"));
                        instanceDetailsTable.Rows[4].Cells[0].Paragraphs[0].Append("Deployed by");
                        instanceDetailsTable.Rows[4].Cells[1].Paragraphs[0].Append(saasInstance.DeployedBy);
                        instanceDetailsTable.Rows[5].Cells[0].Paragraphs[0].Append("Environment admin");
                        instanceDetailsTable.Rows[5].Cells[1].Paragraphs[0].Append(saasInstance.EnvironmentAdmin);
                        instanceDetailsTable.Rows[6].Cells[0].Paragraphs[0].Append("Current application build version");
                        instanceDetailsTable.Rows[6].Cells[1].Paragraphs[0].Append(saasInstance.CurrentApplicationBuildVersion);
                        instanceDetailsTable.Rows[7].Cells[0].Paragraphs[0].Append("Current application release name");
                        instanceDetailsTable.Rows[7].Cells[1].Paragraphs[0].Append(saasInstance.CurrentApplicationReleaseName);
                        instanceDetailsTable.Rows[8].Cells[0].Paragraphs[0].Append("Current platform release name");
                        instanceDetailsTable.Rows[8].Cells[1].Paragraphs[0].Append(saasInstance.CurrentPlatformReleaseName);
                        instanceDetailsTable.Rows[9].Cells[0].Paragraphs[0].Append("Current platform version");
                        instanceDetailsTable.Rows[9].Cells[1].Paragraphs[0].Append(saasInstance.CurrentPlatformVersion);
                        instanceDetailsTable.Rows[10].Cells[0].Paragraphs[0].Append("Number of virtual machines");
                        instanceDetailsTable.Rows[10].Cells[1].Paragraphs[0].Append(saasInstance.VirtualMachineCount.ToString());
                        //Navigation links
                        foreach (var link in saasInstance.NavigationLinks)
                        {
                            Hyperlink navLink = document.AddHyperlink(link.NavigationUri, new Uri(link.NavigationUri));
                            var r = instanceDetailsTable.InsertRow();
                            r.Cells[0].Paragraphs[0].Append(link.DisplayName);
                            r.Cells[1].Paragraphs[0].AppendHyperlink(navLink);
                        }

                        instanceHeader.InsertTableAfterSelf(instanceDetailsTable);

                        var rdpList = _httpClientHelper.GetRdpConnectionDetails(saasInstance);
                        if (rdpList.Count > 0)
                        {
                            var vms = document.InsertParagraph("RDP connections: " + saasInstance.DisplayName.ToUpper()).FontSize(14d);
                            vms.SpacingBefore(20d);
                            vms.SpacingAfter(10d);
                            foreach (var rdpEntry in rdpList)
                            {
                                //RDP details table
                                var columnWidths = new float[] { 300f, 400f };
                                var rdpTable = document.AddTable(4, columnWidths.Length);
                                rdpTable.SetWidths(columnWidths);
                                rdpTable.Design = TableDesign.LightListAccent1;
                                rdpTable.Alignment = Alignment.left;
                                rdpTable.AutoFit = AutoFit.Contents;
                                rdpTable.Rows[0].Cells[0].Paragraphs[0].Append("Machine name");
                                rdpTable.Rows[0].Cells[1].Paragraphs[0].Append(rdpEntry.Machine);
                                rdpTable.Rows[1].Cells[0].Paragraphs[0].Append("RDP address");
                                rdpTable.Rows[1].Cells[1].Paragraphs[0].Append(rdpEntry.Address + ":" + rdpEntry.Port);
                                rdpTable.Rows[2].Cells[0].Paragraphs[0].Append("User name");
                                rdpTable.Rows[2].Cells[1].Paragraphs[0].Append(rdpEntry.Domain + "\\" + rdpEntry.Username);
                                rdpTable.Rows[3].Cells[0].Paragraphs[0].Append("Password");
                                rdpTable.Rows[3].Cells[1].Paragraphs[0].Append(rdpEntry.Password);
                                document.InsertTable(rdpTable);
                                document.InsertParagraph();
                            }
                        }
                        foreach (var vm in saasInstance.Instances)
                        {
                            var CredentialsDict = _httpClientHelper.GetCredentials(saasInstance.EnvironmentId, vm.ItemName);
                            if (CredentialsDict.Count > 0)
                            {
                                var credentialsParagraph = document.InsertParagraph("Credentials for " + vm.MachineName).FontSize(14d);
                                credentialsParagraph.SpacingBefore(20d);
                                credentialsParagraph.SpacingAfter(10d);
                                // CHE credentials table
                                var columnWidths = new float[] { 150f, 150f };
                                var credentialsTable = document.AddTable(1, columnWidths.Length);
                                credentialsTable.SetWidths(columnWidths);
                                credentialsTable.Design = TableDesign.LightListAccent3;
                                credentialsTable.Alignment = Alignment.left;
                                credentialsTable.AutoFit = AutoFit.Contents;
                                credentialsTable.Rows[0].Cells[0].Paragraphs[0].Append("User name");
                                credentialsTable.Rows[0].Cells[1].Paragraphs[0].Append("Password");

                                foreach (var credential in CredentialsDict)
                                {
                                    var r = credentialsTable.InsertRow();
                                    r.Cells[0].Paragraphs[0].Append(credential.Key
                                        .Replace("Dev-Local admin-", "")
                                        .Replace("Dev-Local user-", "")
                                        .Replace("Dev-Sql server login-", "")
                                        .Replace("Build-Local user-", "")
                                        .Replace("Build-Sql server login-", "")
                                        .Replace("AOS-Local admin-", "")
                                        .Replace("BI-Local admin-", "")
                                        .Replace("AD-AosServiceUser-", "")
                                        .Replace("AD-SqlServiceUser-", "")
                                        .Replace("AD-DynamicsInstallUser-", "")
                                        .Replace("AD-SPServiceUser-", "")
                                        .Replace("AD-BCProxyUser-", "")
                                        .Replace("AD-Local admin-", ""));
                                    r.Cells[1].Paragraphs[0].Append(credential.Value);
                                }
                                credentialsParagraph.InsertTableAfterSelf(credentialsTable);
                            }
                        }
                        document.InsertParagraph().InsertPageBreakAfterSelf();
                    }
                }

                if (_cheInstancesList != null && _cheInstancesList.Count > 0)
                {
                    var instancesHeader = document.InsertParagraph("Cloud hosted instances").Heading(HeadingType.Heading1).FontSize(20d);
                    instancesHeader.SpacingAfter(40d);

                    foreach (var instance in _cheInstancesList)
                    {
                        //instance header
                        var instanceHeader = document.InsertParagraph(instance.DisplayName).CapsStyle(CapsStyle.caps).Heading(HeadingType.Heading2).FontSize(16d);
                        instanceHeader.SpacingAfter(10d);

                        //instance table
                        var instanceColumnWidths = new float[] { 200f, 400f };
                        var instanceDetailsTable = document.AddTable(10, instanceColumnWidths.Length);
                        instanceDetailsTable.SetWidths(instanceColumnWidths);
                        instanceDetailsTable.Design = TableDesign.LightListAccent2;
                        instanceDetailsTable.Alignment = Alignment.left;
                        instanceDetailsTable.Rows[0].Cells[0].Paragraphs[0].Append("Name");
                        instanceDetailsTable.Rows[0].Cells[1].Paragraphs[0].Append(instance.DisplayName);
                        instanceDetailsTable.Rows[1].Cells[0].Paragraphs[0].Append("Environment Id");
                        instanceDetailsTable.Rows[1].Cells[1].Paragraphs[0].Append(instance.EnvironmentId);
                        instanceDetailsTable.Rows[2].Cells[0].Paragraphs[0].Append("Topology");
                        instanceDetailsTable.Rows[2].Cells[1].Paragraphs[0].Append(instance.TopologyDisplayName);
                        instanceDetailsTable.Rows[3].Cells[0].Paragraphs[0].Append("Deployed on");
                        instanceDetailsTable.Rows[3].Cells[1].Paragraphs[0].Append(instance.DeployedOn); //Todo ?.ToString("yyyy-MM-dd H:mm"));
                        instanceDetailsTable.Rows[4].Cells[0].Paragraphs[0].Append("Deployed by");
                        instanceDetailsTable.Rows[4].Cells[1].Paragraphs[0].Append(instance.DeployedBy);
                        instanceDetailsTable.Rows[5].Cells[0].Paragraphs[0].Append("Environment admin");
                        instanceDetailsTable.Rows[5].Cells[1].Paragraphs[0].Append(instance.EnvironmentAdmin);
                        instanceDetailsTable.Rows[6].Cells[0].Paragraphs[0].Append("Current application build version");
                        instanceDetailsTable.Rows[6].Cells[1].Paragraphs[0].Append(instance.CurrentApplicationBuildVersion);
                        instanceDetailsTable.Rows[7].Cells[0].Paragraphs[0].Append("Current application release name");
                        instanceDetailsTable.Rows[7].Cells[1].Paragraphs[0].Append(instance.CurrentApplicationReleaseName);
                        instanceDetailsTable.Rows[8].Cells[0].Paragraphs[0].Append("Current platform release name");
                        instanceDetailsTable.Rows[8].Cells[1].Paragraphs[0].Append(instance.CurrentPlatformReleaseName);
                        instanceDetailsTable.Rows[9].Cells[0].Paragraphs[0].Append("Current platform version");
                        instanceDetailsTable.Rows[9].Cells[1].Paragraphs[0].Append(instance.CurrentPlatformVersion);
                        //Navigation links
                        foreach (var link in instance.NavigationLinks)
                        {
                            Hyperlink navLink = document.AddHyperlink(link.NavigationUri, new Uri(link.NavigationUri));
                            var r = instanceDetailsTable.InsertRow();
                            r.Cells[0].Paragraphs[0].Append(link.DisplayName);
                            r.Cells[1].Paragraphs[0].AppendHyperlink(navLink);
                        }

                        instanceHeader.InsertTableAfterSelf(instanceDetailsTable);
                        var rdpList = _httpClientHelper.GetRdpConnectionDetails(instance);
                        if (rdpList.Count > 0)
                        {
                            var vms = document.InsertParagraph("RDP connections: " + instance.DisplayName.ToUpper()).FontSize(14d);
                            vms.SpacingBefore(20d);
                            vms.SpacingAfter(10d);
                            foreach (var rdpEntry in rdpList)
                            {
                                //RDP details table
                                var columnWidths = new float[] { 300f, 400f };
                                var rdpTable = document.AddTable(3, columnWidths.Length);
                                rdpTable.SetWidths(columnWidths);
                                rdpTable.Design = TableDesign.LightListAccent1;
                                rdpTable.Alignment = Alignment.left;
                                rdpTable.AutoFit = AutoFit.Contents;
                                rdpTable.Rows[0].Cells[0].Paragraphs[0].Append("RDP address");
                                rdpTable.Rows[0].Cells[1].Paragraphs[0].Append(rdpEntry.Address + ":" + rdpEntry.Port);
                                rdpTable.Rows[1].Cells[0].Paragraphs[0].Append("User name");
                                rdpTable.Rows[1].Cells[1].Paragraphs[0].Append(rdpEntry.Domain + "\\" + rdpEntry.Username);
                                rdpTable.Rows[2].Cells[0].Paragraphs[0].Append("Password");
                                rdpTable.Rows[2].Cells[1].Paragraphs[0].Append(rdpEntry.Password);
                                document.InsertTable(rdpTable);
                                document.InsertParagraph();
                            }
                        }
                        foreach (var vm in instance.Instances)
                        {
                            var CredentialsDict = _httpClientHelper.GetCredentials(instance.EnvironmentId, vm.ItemName);
                            if (CredentialsDict.Count > 0)
                            {
                                var credentialsParagraph = document.InsertParagraph("Credentials").FontSize(14d);
                                credentialsParagraph.SpacingBefore(20d);
                                credentialsParagraph.SpacingAfter(10d);
                                // CHE credentials table
                                var columnWidths = new float[] { 150f, 150f };
                                var credentialsTable = document.AddTable(1, columnWidths.Length);
                                credentialsTable.SetWidths(columnWidths);
                                credentialsTable.Design = TableDesign.LightListAccent3;
                                credentialsTable.Alignment = Alignment.left;
                                credentialsTable.AutoFit = AutoFit.Contents;
                                credentialsTable.Rows[0].Cells[0].Paragraphs[0].Append("User name");
                                credentialsTable.Rows[0].Cells[1].Paragraphs[0].Append("Password");

                                foreach (var credential in CredentialsDict)
                                {
                                    var r = credentialsTable.InsertRow();
                                    r.Cells[0].Paragraphs[0].Append(credential.Key
                                        .Replace("Dev-Local admin-", "")
                                        .Replace("Dev-Local user-", "")
                                        .Replace("Dev-Sql server login-", "")
                                        .Replace("Build-Local user-", "")
                                        .Replace("Build-Sql server login-", "")
                                        .Replace("AOS-Local admin-", "")
                                        .Replace("BI-Local admin-", "")
                                        .Replace("AD-AosServiceUser-", "")
                                        .Replace("AD-SqlServiceUser-", "")
                                        .Replace("AD-DynamicsInstallUser-", "")
                                        .Replace("AD-SPServiceUser-", "")
                                        .Replace("AD-BCProxyUser-", "")
                                        .Replace("AD-Local admin-", ""));
                                    r.Cells[1].Paragraphs[0].Append(credential.Value);
                                }
                                credentialsParagraph.InsertTableAfterSelf(credentialsTable).InsertPageBreakAfterSelf();
                            }
                        }
                    }
                }

                var savefile = new SaveFileDialog
                {
                    FileName = _selectedProject.Name + " - 2LCS generated.docx",
                    Filter = "Word document (*.docx)|*.docx|All files (*.*)|*.*",
                    DefaultExt = "docx",
                    AddExtension = true
                };
                if (savefile.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        document.SaveAs(savefile.FileName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            Cursor = Cursors.Default;
        }