diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 30bd1ce..ec79d6d 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -158,6 +158,9 @@ function Move-Files { } } function Get-FolderBrowser { + param ( + [string]$ParentFolderUrl = "" + ) try { # Get site ID first $siteUrl = $script:txtSiteUrl.Text.TrimEnd('/') @@ -188,7 +191,28 @@ function Get-FolderBrowser { } # Get root folder first - $root = Get-MgDriveRoot -DriveId $drive.Id + # Get root folder first + 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 + $relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/") + + # URL encode the relative path + $encodedPath = [System.Web.HttpUtility]::UrlEncode($relativePath) + + $folderUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$encodedPath" + $script:txtStatus.Text += "Folder URI: $folderUri`n" + try { + $folderItem = Invoke-MgGraphRequest -Method GET -Uri $folderUri -ErrorAction Stop + } catch { + throw "Folder not found: $($_.Exception.Message)" + } + $root = $folderItem + } + else { + $root = Get-MgDriveRoot -DriveId $drive.Id + } # Get folders using the root item's ID $folders = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id | Where-Object { $_.Folder } @@ -198,11 +222,33 @@ function Get-FolderBrowser { [PSCustomObject]@{ Name = $_.Name ServerRelativeUrl = "/sites/tax/Shared Documents/$($_.Name)" + IsFolder = $true # Add a property to indicate it's a folder } } - return $folderObjects | - Out-GridView -Title "Select Folder" -PassThru + # Add ".." entry to go up a level if not at the root + if ($ParentFolderUrl) { + $parentUrl = Split-Path -Path $ParentFolderUrl -Parent + $folderObjects = @([PSCustomObject]@{ + Name = ".." + ServerRelativeUrl = $parentUrl + IsFolder = $true + }) + $folderObjects + } + + # Show the Out-GridView + $selected = $folderObjects | Out-GridView -Title "Select Folder" -PassThru + + # If a folder is selected, browse into it + if ($selected -and $selected.IsFolder -eq $true -and $selected.Name -ne "..") { + return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl + } + elseif ($selected -and $selected.Name -eq "..") { + return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl + } + else { + return $selected + } } catch { $script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"