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.Multiline = $true
|
||||||
$txtStatus.ScrollBars = "Vertical"
|
$txtStatus.ScrollBars = "Vertical"
|
||||||
$form.Controls.Add($txtStatus)
|
$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(@(
|
$form.Controls.AddRange(@(
|
||||||
$txtSiteUrl,
|
$txtSiteUrl,
|
||||||
$txtProdLib,
|
$txtProdLib,
|
||||||
$txtTempLib,
|
$txtTempLib,
|
||||||
$txtFolder,
|
$txtFolder,
|
||||||
$btnBrowse
|
$btnBrowse,
|
||||||
|
$btnSaveConfig
|
||||||
))
|
))
|
||||||
|
|
||||||
# Load configuration
|
# Load configuration
|
||||||
@ -89,10 +103,21 @@ $btnList.Add_Click({ List-XlsFiles })
|
|||||||
$btnConvert.Add_Click({ Convert-Files })
|
$btnConvert.Add_Click({ Convert-Files })
|
||||||
$btnMove.Add_Click({ Move-Files })
|
$btnMove.Add_Click({ Move-Files })
|
||||||
$btnBrowse.Add_Click({
|
$btnBrowse.Add_Click({
|
||||||
$selectedFolder = Get-FolderBrowser -context (Get-PnPConnection)
|
try {
|
||||||
if ($selectedFolder) {
|
if (-not (Connect-SharePoint)) {
|
||||||
$relativePath = $selectedFolder.ServerRelativeUrl -replace [regex]::Escape((Get-PnPWeb).ServerRelativeUrl), ""
|
[System.Windows.Forms.MessageBox]::Show("Please configure and connect to SharePoint first!")
|
||||||
$script:txtFolder.Text = $relativePath.Trim('/')
|
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 {
|
function Connect-SharePoint {
|
||||||
try {
|
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
|
return $true
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@ -69,7 +78,7 @@ function List-XlsFiles {
|
|||||||
foreach ($item in $items) {
|
foreach ($item in $items) {
|
||||||
$fileList += [PSCustomObject]@{
|
$fileList += [PSCustomObject]@{
|
||||||
OriginalPath = $item.FieldValues.FileRef
|
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 {
|
function Get-FolderBrowser {
|
||||||
param($context)
|
param($context)
|
||||||
try {
|
try {
|
||||||
$folder = Get-PnPFolder -Url $script:txtProdLib.Text
|
if (-not (Connect-SharePoint)) {
|
||||||
$ctx = $context
|
return $null
|
||||||
$folders = @()
|
}
|
||||||
|
|
||||||
$fld = $ctx.Web.GetFolderByServerRelativePath($folder.ServerRelativeUrl)
|
$libraryName = $script:txtProdLib.Text
|
||||||
$ctx.Load($fld.Folders)
|
if (-not $libraryName) {
|
||||||
$ctx.ExecuteQuery()
|
throw "Production Library name is required"
|
||||||
|
}
|
||||||
|
|
||||||
$folders += $fld.Folders | Select-Object Name, ServerRelativeUrl
|
$ctx = Get-PnPConnection
|
||||||
return $folders | Out-GridView -Title "Select Folder" -PassThru
|
$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 {
|
catch {
|
||||||
$script:txtStatus.Text += "Error browsing folders: $_`n"
|
$script:txtStatus.Text += "Error browsing folders: $_`n"
|
||||||
|
return $null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user