fix: Update SharePoint file download/upload to use proper drive ID
This commit is contained in:
parent
728ce14af4
commit
529c0f3982
@ -420,16 +420,31 @@ function Convert-Files {
|
|||||||
# Download the file from SharePoint
|
# Download the file from SharePoint
|
||||||
$downloadPath = "$env:TEMP\$originalFileName"
|
$downloadPath = "$env:TEMP\$originalFileName"
|
||||||
try {
|
try {
|
||||||
# Extract DriveId and ItemId from the originalPath
|
# Get the site first
|
||||||
if ($originalPath -match "drives/([^/]+)/items/([^/]+)") {
|
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
||||||
$driveId = $matches[1]
|
|
||||||
$itemId = $matches[2]
|
# Get the drive
|
||||||
} else {
|
$drives = Get-MgSiteDrive -SiteId $site.id
|
||||||
throw "Could not extract DriveId and ItemId from OriginalPath"
|
$libraryName = if ($script:txtProdLib.Text -eq "Shared Documents") {
|
||||||
|
"Documents"
|
||||||
|
} else {
|
||||||
|
$script:txtProdLib.Text
|
||||||
|
}
|
||||||
|
$drive = $drives | Where-Object { $_.Name -eq $libraryName }
|
||||||
|
|
||||||
|
# Get the file path relative to the library root
|
||||||
|
$relativePath = $originalFileName
|
||||||
|
if ($script:txtFolder.Text) {
|
||||||
|
$folderPath = $script:txtFolder.Text.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
|
||||||
|
$relativePath = "$folderPath/$originalFileName"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Construct the download URI
|
||||||
|
$encodedPath = [System.Web.HttpUtility]::UrlEncode($relativePath).Replace("+", "%20")
|
||||||
|
$downloadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$encodedPath:/content"
|
||||||
|
|
||||||
# Download the file
|
# Download the file
|
||||||
$fileContent = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$itemId/content" -Method GET -OutFile $downloadPath
|
Invoke-MgGraphRequest -Uri $downloadUri -Method GET -OutFile $downloadPath
|
||||||
if (-not (Test-Path $downloadPath)) {
|
if (-not (Test-Path $downloadPath)) {
|
||||||
throw "Failed to download file from SharePoint"
|
throw "Failed to download file from SharePoint"
|
||||||
}
|
}
|
||||||
@ -475,9 +490,9 @@ function Convert-Files {
|
|||||||
|
|
||||||
# Upload the converted file to SharePoint
|
# Upload the converted file to SharePoint
|
||||||
try {
|
try {
|
||||||
# Get the Drive ID and destination folder
|
# Get the destination folder
|
||||||
$destinationFolder = $script:txtTempLib.Text
|
$destinationFolder = $script:txtTempLib.Text
|
||||||
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$driveId/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
||||||
|
|
||||||
# Read the file content
|
# Read the file content
|
||||||
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user