From c30d7ccdc56675945f2e4167c13ca92149f0301f Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Fri, 21 Feb 2025 09:30:59 -0800 Subject: [PATCH] refactor: Replace `Out-GridView` with Form and ListBox in `Get-FolderBrowser` --- SharePointFunctions.ps1 | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index ec79d6d..de97f89 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -195,7 +195,7 @@ function Get-FolderBrowser { if ($ParentFolderUrl) { # Get the folder ID first # Construct the correct URI for the folder - # Remove the /sites/tax/Shared Documents part from the folder path + # Remove the /sites/tax/Shared Documents/ part from the folder path $relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/") # URL encode the relative path @@ -236,8 +236,49 @@ function Get-FolderBrowser { }) + $folderObjects } - # Show the Out-GridView - $selected = $folderObjects | Out-GridView -Title "Select Folder" -PassThru + # Create a form and listbox for folder selection + $form = New-Object System.Windows.Forms.Form + $form.Text = "Select Folder" + $form.Size = New-Object System.Drawing.Size(400, 400) + $listBox = New-Object System.Windows.Forms.ListBox + $listBox.Size = New-Object System.Drawing.Size(360, 320) + $listBox.Location = New-Object System.Drawing.Point(10, 10) + $okButton = New-Object System.Windows.Forms.Button + $okButton.Location = New-Object System.Drawing.Point(150, 340) + $okButton.Size = New-Object System.Drawing.Size(75, 23) + $okButton.Text = "OK" + $cancelButton = New-Object System.Windows.Forms.Button + $cancelButton.Location = New-Object System.Drawing.Point(230, 340) + $cancelButton.Size = New-Object System.Drawing.Size(75, 23) + $cancelButton.Text = "Cancel" + $form.Controls.Add($listBox) | Out-Null + $form.Controls.Add($okButton) | Out-Null + $form.Controls.Add($cancelButton) | Out-Null + $form.FormBorderStyle = 'FixedDialog' + $form.StartPosition = 'CenterScreen' + $form.Add_Shown({$Form.Activate()}) + # Populate listbox + foreach ($folder in $folderObjects) { + $listBox.Items.Add($folder.Name) | Out-Null + } + + # Handle OK button click + $okButton.Add_Click({ + $form.Tag = $listBox.SelectedItem + $form.Close() + }) + + # Handle Cancel button click + $cancelButton.Add_Click({ + $form.Tag = $null + $form.Close() + }) + + # Show the Form as a Dialog + [void]$form.ShowDialog() + + # Get the selected folder + $selected = $folderObjects | Where-Object {$_.Name -eq $form.Tag} # If a folder is selected, browse into it if ($selected -and $selected.IsFolder -eq $true -and $selected.Name -ne "..") { @@ -246,9 +287,12 @@ function Get-FolderBrowser { elseif ($selected -and $selected.Name -eq "..") { return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl } - else { + elseif ($selected) { return $selected } + else { + return $null + } } catch { $script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"