Add-Type -AssemblyName System.Windows.Forms Import-Module Microsoft.Graph.Sites Import-Module Microsoft.Graph.Files # Import our components . "$PSScriptRoot\GraphConfig.ps1" . "$PSScriptRoot\SharePointFunctions.ps1" . "$PSScriptRoot\EventHandlers.ps1" # Create main form $form = New-Object System.Windows.Forms.Form $form.Text = "SharePoint XLS Converter" $form.Size = New-Object System.Drawing.Size(800, 600) # Increased form size # Set AutoScaleMode to Dpi $form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi # Add controls $lblConfig = New-Object System.Windows.Forms.Label $lblConfig.Text = "SharePoint Configuration:" $lblConfig.Location = New-Object System.Drawing.Point(20, 20) $lblConfig.Size = New-Object System.Drawing.Size(200, 20) $form.Controls.Add($lblConfig) $txtSiteUrl = New-Object System.Windows.Forms.TextBox $txtSiteUrl.Location = New-Object System.Drawing.Point(20, 40) $txtSiteUrl.Size = New-Object System.Drawing.Size(200, 20) $txtSiteUrl.PlaceholderText = "Site URL" $txtProdLib = New-Object System.Windows.Forms.TextBox $txtProdLib.Location = New-Object System.Drawing.Point(20, 70) $txtProdLib.Size = New-Object System.Drawing.Size(200, 20) $txtProdLib.PlaceholderText = "Production Library" $txtTempLib = New-Object System.Windows.Forms.TextBox $txtTempLib.Location = New-Object System.Drawing.Point(20, 100) $txtTempLib.Size = New-Object System.Drawing.Size(200, 20) $txtTempLib.PlaceholderText = "Temp Library" $txtFolder = New-Object System.Windows.Forms.TextBox $txtFolder.Location = New-Object System.Drawing.Point(20, 130) $txtFolder.Size = New-Object System.Drawing.Size(200, 20) $txtFolder.PlaceholderText = "Folder Path (optional)" $btnBrowse = New-Object System.Windows.Forms.Button $btnBrowse.Location = New-Object System.Drawing.Point(230, 130) $btnBrowse.Size = New-Object System.Drawing.Size(80, 20) $btnBrowse.Text = "Browse" $btnList = New-Object System.Windows.Forms.Button $btnList.Location = New-Object System.Drawing.Point(20, 170) $btnList.Size = New-Object System.Drawing.Size(120, 30) $btnList.Text = "List XLS Files" $form.Controls.Add($btnList) $btnConvert = New-Object System.Windows.Forms.Button $btnConvert.Location = New-Object System.Drawing.Point(160, 170) $btnConvert.Size = New-Object System.Drawing.Size(120, 30) $btnConvert.Text = "Convert to XLSX" $form.Controls.Add($btnConvert) $btnMove = New-Object System.Windows.Forms.Button $btnMove.Location = New-Object System.Drawing.Point(300, 170) $btnMove.Size = New-Object System.Drawing.Size(120, 30) $btnMove.Text = "Move Files" $form.Controls.Add($btnMove) # Create Status TextBox (Top Right) $txtStatus = New-Object System.Windows.Forms.TextBox $txtStatus.Location = New-Object System.Drawing.Point(450, 20) # Top Right Corner $txtStatus.Size = New-Object System.Drawing.Size(330, 180) # Adjusted size $txtStatus.Multiline = $true $txtStatus.ScrollBars = "Vertical" $txtStatus.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor ` [System.Windows.Forms.AnchorStyles]::Right $form.Controls.Add($txtStatus) # Create DataGridView (Lower 2/3) $dataGridView = New-Object System.Windows.Forms.DataGridView $dataGridView.Location = New-Object System.Drawing.Point(20, 210) $dataGridView.Size = New-Object System.Drawing.Size(760, 340) # Lower 2/3 of the form $dataGridView.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor ` [System.Windows.Forms.AnchorStyles]::Left -bor ` [System.Windows.Forms.AnchorStyles]::Right -bor ` [System.Windows.Forms.AnchorStyles]::Top $dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill $form.Controls.Add($dataGridView) # Add columns to DataGridView $dataGridView.ColumnCount = 3 $dataGridView.Columns[0].Name = "Original Path" $dataGridView.Columns[1].Name = "Original File Name" $dataGridView.Columns[2].Name = "Temp Path" # Add Save Config button $btnSaveConfig = New-Object System.Windows.Forms.Button $btnSaveConfig.Location = New-Object System.Drawing.Point(230, 40) $btnSaveConfig.Size = New-Object System.Drawing.Size(80, 20) $btnSaveConfig.Text = "Save Config" $btnSaveConfig.Add_Click({ Save-Config -SiteUrl $txtSiteUrl.Text ` -ProductionLibrary $txtProdLib.Text ` -TempLibrary $txtTempLib.Text }) # Add Connect button $btnConnect = New-Object System.Windows.Forms.Button $btnConnect.Location = New-Object System.Drawing.Point(320, 40) $btnConnect.Size = New-Object System.Drawing.Size(80, 20) $btnConnect.Text = "Connect" $btnConnect.Add_Click({ if (Connect-SharePoint) { $script:txtStatus.Text += "Successfully connected to SharePoint`n" } }) $form.Controls.AddRange(@( $txtSiteUrl, $txtProdLib, $txtTempLib, $txtFolder, $btnBrowse, $btnSaveConfig, $btnConnect )) # Load configuration if (-not (Load-Config)) { [System.Windows.Forms.MessageBox]::Show("Configuration missing! Please enter settings.") } else { $txtSiteUrl.Text = $config.SiteUrl $txtProdLib.Text = $config.ProductionLibrary $txtTempLib.Text = $config.TempLibrary } # Attach event handlers $btnList.Add_Click({ List-XlsFiles_Click }) $btnConvert.Add_Click({ Convert-Files_Click }) $btnMove.Add_Click({ Move-Files_Click }) $btnBrowse.Add_Click({ try { if (-not (Connect-SharePoint)) { [System.Windows.Forms.MessageBox]::Show("Please configure and connect to SharePoint first!") return } $selectedFolder = Get-FolderBrowser if ($selectedFolder) { # Use the ServerRelativeUrl directly from the folder browser $script:txtFolder.Text = $selectedFolder.ServerRelativeUrl } } catch { $script:txtStatus.Text += "Folder browse error: $_`n" } }) # Store the DataGridView and txtStatus in script scope $script:dataGridView = $dataGridView $script:txtStatus = $txtStatus # Show form $form.Add_Shown({ $form.Activate() }) [void]$form.ShowDialog()