fix: Improve SharePoint connection handling and folder browser reliability
This commit is contained in:
parent
b2a495af78
commit
2291118f0c
35
MainForm.ps1
35
MainForm.ps1
@ -66,12 +66,26 @@ $txtStatus.Size = New-Object System.Drawing.Size(540,240)
|
||||
$txtStatus.Multiline = $true
|
||||
$txtStatus.ScrollBars = "Vertical"
|
||||
$form.Controls.Add($txtStatus)
|
||||
# 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 `
|
||||
-ClientId $config.ClientId `
|
||||
-ClientSecret $config.ClientSecret
|
||||
})
|
||||
|
||||
$form.Controls.AddRange(@(
|
||||
$txtSiteUrl,
|
||||
$txtProdLib,
|
||||
$txtTempLib,
|
||||
$txtFolder,
|
||||
$btnBrowse
|
||||
$btnBrowse,
|
||||
$btnSaveConfig
|
||||
))
|
||||
|
||||
# Load configuration
|
||||
@ -89,10 +103,21 @@ $btnList.Add_Click({ List-XlsFiles })
|
||||
$btnConvert.Add_Click({ Convert-Files })
|
||||
$btnMove.Add_Click({ Move-Files })
|
||||
$btnBrowse.Add_Click({
|
||||
$selectedFolder = Get-FolderBrowser -context (Get-PnPConnection)
|
||||
if ($selectedFolder) {
|
||||
$relativePath = $selectedFolder.ServerRelativeUrl -replace [regex]::Escape((Get-PnPWeb).ServerRelativeUrl), ""
|
||||
$script:txtFolder.Text = $relativePath.Trim('/')
|
||||
try {
|
||||
if (-not (Connect-SharePoint)) {
|
||||
[System.Windows.Forms.MessageBox]::Show("Please configure and connect to SharePoint first!")
|
||||
return
|
||||
}
|
||||
|
||||
$selectedFolder = Get-FolderBrowser
|
||||
if ($selectedFolder) {
|
||||
$webUrl = (Get-PnPWeb).ServerRelativeUrl
|
||||
$relativePath = $selectedFolder.ServerRelativeUrl -replace [regex]::Escape($webUrl), ""
|
||||
$script:txtFolder.Text = $relativePath.Trim('/')
|
||||
}
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Folder browse error: $_`n"
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@ -37,7 +37,16 @@ function Save-Config {
|
||||
|
||||
function Connect-SharePoint {
|
||||
try {
|
||||
Connect-PnPOnline -Url $config.SiteUrl -ClientId $config.ClientId -ClientSecret $config.ClientSecret
|
||||
# Use current form values instead of saved config
|
||||
$siteUrl = $script:txtSiteUrl.Text
|
||||
$clientId = $config.ClientId
|
||||
$clientSecret = $config.ClientSecret
|
||||
|
||||
if (-not $siteUrl) {
|
||||
throw "Please enter a Site URL"
|
||||
}
|
||||
|
||||
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret -WarningAction SilentlyContinue
|
||||
return $true
|
||||
}
|
||||
catch {
|
||||
@ -69,7 +78,7 @@ function List-XlsFiles {
|
||||
foreach ($item in $items) {
|
||||
$fileList += [PSCustomObject]@{
|
||||
OriginalPath = $item.FieldValues.FileRef
|
||||
TempPath = "$tempLibrary/$($item.FieldValues.FileLeafRef)"
|
||||
TempPath = "$($script:txtTempLib.Text)/$($item.FieldValues.FileLeafRef)"
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,18 +133,27 @@ function Move-Files {
|
||||
function Get-FolderBrowser {
|
||||
param($context)
|
||||
try {
|
||||
$folder = Get-PnPFolder -Url $script:txtProdLib.Text
|
||||
$ctx = $context
|
||||
$folders = @()
|
||||
if (-not (Connect-SharePoint)) {
|
||||
return $null
|
||||
}
|
||||
|
||||
$fld = $ctx.Web.GetFolderByServerRelativePath($folder.ServerRelativeUrl)
|
||||
$ctx.Load($fld.Folders)
|
||||
$ctx.ExecuteQuery()
|
||||
$libraryName = $script:txtProdLib.Text
|
||||
if (-not $libraryName) {
|
||||
throw "Production Library name is required"
|
||||
}
|
||||
|
||||
$folders += $fld.Folders | Select-Object Name, ServerRelativeUrl
|
||||
return $folders | Out-GridView -Title "Select Folder" -PassThru
|
||||
$ctx = Get-PnPConnection
|
||||
$web = Get-PnPWeb
|
||||
$library = Get-PnPList -Identity $libraryName
|
||||
|
||||
$folders = Get-PnPFolder -Url $library.RootFolder.ServerRelativeUrl -Includes Folders |
|
||||
Select-Object -ExpandProperty Folders |
|
||||
Select-Object Name, ServerRelativeUrl
|
||||
|
||||
return $folders | Out-GridView -Title "Select Folder (Double-click to select)" -PassThru
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Error browsing folders: $_`n"
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user