feat: Add folder browsing and dynamic library configuration fields

This commit is contained in:
Bobby Abellana (aider) 2025-02-19 14:30:25 -08:00
parent 77e4db0d78
commit b2a495af78
2 changed files with 86 additions and 21 deletions

View File

@ -8,54 +8,93 @@ Import-Module PnP.PowerShell
# Create main form
$form = New-Object System.Windows.Forms.Form
$form.Text = "SharePoint XLS Converter"
$form.Size = New-Object System.Drawing.Size(600,400)
$form.Size = New-Object System.Drawing.Size(600,500)
# Add controls
$txtSiteUrl = New-Object System.Windows.Forms.TextBox
$txtSiteUrl.Location = New-Object System.Drawing.Point(20,120)
$txtSiteUrl.Size = New-Object System.Drawing.Size(200,20)
$lblConfig = New-Object System.Windows.Forms.Label
$lblConfig.Text = "SharePoint Configuration:"
$lblConfig.Location = New-Object System.Drawing.Point(20,20)
$lblConfig.Size = New-Object System.Drawing.Size(200,20)
$form.Controls.Add($lblConfig)
$btnSaveConfig = New-Object System.Windows.Forms.Button
$btnSaveConfig.Location = New-Object System.Drawing.Point(230,120)
$btnSaveConfig.Size = New-Object System.Drawing.Size(80,20)
$btnSaveConfig.Text = "Save Config"
$txtSiteUrl = New-Object System.Windows.Forms.TextBox
$txtSiteUrl.Location = New-Object System.Drawing.Point(20,40)
$txtSiteUrl.Size = New-Object System.Drawing.Size(200,20)
$txtSiteUrl.PlaceholderText = "Site URL"
$txtProdLib = New-Object System.Windows.Forms.TextBox
$txtProdLib.Location = New-Object System.Drawing.Point(20,70)
$txtProdLib.Size = New-Object System.Drawing.Size(200,20)
$txtProdLib.PlaceholderText = "Production Library"
$txtTempLib = New-Object System.Windows.Forms.TextBox
$txtTempLib.Location = New-Object System.Drawing.Point(20,100)
$txtTempLib.Size = New-Object System.Drawing.Size(200,20)
$txtTempLib.PlaceholderText = "Temp Library"
$txtFolder = New-Object System.Windows.Forms.TextBox
$txtFolder.Location = New-Object System.Drawing.Point(20,130)
$txtFolder.Size = New-Object System.Drawing.Size(200,20)
$txtFolder.PlaceholderText = "Folder Path (optional)"
$btnBrowse = New-Object System.Windows.Forms.Button
$btnBrowse.Location = New-Object System.Drawing.Point(230,130)
$btnBrowse.Size = New-Object System.Drawing.Size(80,20)
$btnBrowse.Text = "Browse"
$btnList = New-Object System.Windows.Forms.Button
$btnList.Location = New-Object System.Drawing.Point(20,20)
$btnList.Location = New-Object System.Drawing.Point(20,170)
$btnList.Size = New-Object System.Drawing.Size(120,30)
$btnList.Text = "List XLS Files"
$form.Controls.Add($btnList)
$btnConvert = New-Object System.Windows.Forms.Button
$btnConvert.Location = New-Object System.Drawing.Point(160,20)
$btnConvert.Location = New-Object System.Drawing.Point(160,170)
$btnConvert.Size = New-Object System.Drawing.Size(120,30)
$btnConvert.Text = "Convert to XLSX"
$form.Controls.Add($btnConvert)
$btnMove = New-Object System.Windows.Forms.Button
$btnMove.Location = New-Object System.Drawing.Point(300,20)
$btnMove.Location = New-Object System.Drawing.Point(300,170)
$btnMove.Size = New-Object System.Drawing.Size(120,30)
$btnMove.Text = "Move Files"
$form.Controls.Add($btnMove)
$txtStatus = New-Object System.Windows.Forms.TextBox
$txtStatus.Location = New-Object System.Drawing.Point(20,70)
$txtStatus.Size = New-Object System.Drawing.Size(540,280)
$txtStatus.Location = New-Object System.Drawing.Point(20,210)
$txtStatus.Size = New-Object System.Drawing.Size(540,240)
$txtStatus.Multiline = $true
$txtStatus.ScrollBars = "Vertical"
$form.Controls.Add($txtStatus)
$form.Controls.Add($txtSiteUrl)
$form.Controls.Add($btnSaveConfig)
$form.Controls.AddRange(@(
$txtSiteUrl,
$txtProdLib,
$txtTempLib,
$txtFolder,
$btnBrowse
))
# Load configuration
if (-not (Load-Config)) {
[System.Windows.Forms.MessageBox]::Show("Configuration missing! Please enter settings.")
}
else {
$txtSiteUrl.Text = $config.SiteUrl
$txtProdLib.Text = $config.ProductionLibrary
$txtTempLib.Text = $config.TempLibrary
}
# Attach event handlers
$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('/')
}
})
# Show form
$form.Add_Shown({$form.Activate()})

View File

@ -51,11 +51,19 @@ function List-XlsFiles {
return
}
$tempLibrary = $config.TempLibrary
$fileList = @()
$folderPath = $script:txtFolder.Text
$query = "<View Scope='RecursiveAll'><Query><Where>
<And>
<Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xls</Value></Contains>
<Not><Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xlsx</Value></Contains></Not>
</And>
</Where></Query></View>"
$items = Get-PnPListItem -List $config.ProductionLibrary -PageSize 2000 | Where {
($_.FieldValues.FileRef -like "*.xls") -and (-not $_.FieldValues.FileRef.Contains(".xlsx"))
$items = Get-PnPListItem -List $script:txtProdLib.Text -PageSize 2000 -Query $query
if (-not [string]::IsNullOrWhiteSpace($folderPath)) {
$folderFilter = "*$($folderPath.Replace('\','/'))*"
$items = $items | Where { $_.FieldValues.FileRef -like $folderFilter }
}
foreach ($item in $items) {
@ -78,7 +86,7 @@ function Convert-Files {
$excel.Visible = $false
try {
Get-PnPFile -Url "/sites/yoursite/Documents/Temp/FileList.json" -Path $env:TEMP -Filename "FileList.json"
Get-PnPFile -Url "/sites/yoursite/$($script:txtTempLib.Text)/FileList.json" -Path $env:TEMP -Filename "FileList.json"
$files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json
foreach ($file in $files) {
@ -109,7 +117,25 @@ function Move-Files {
foreach ($file in $files) {
Move-PnPFile -SourceUrl $file.OriginalPath -TargetUrl $file.TempPath -Force
$xlsxPath = [System.IO.Path]::ChangeExtension($file.OriginalPath, ".xlsx")
Move-PnPFile -SourceUrl "$tempLibrary/$xlsxPath" -TargetUrl $xlsxPath -Force
Move-PnPFile -SourceUrl "$($script:txtProdLib.Text)/$xlsxPath" -TargetUrl $xlsxPath -Force
$script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n"
}
}
function Get-FolderBrowser {
param($context)
try {
$folder = Get-PnPFolder -Url $script:txtProdLib.Text
$ctx = $context
$folders = @()
$fld = $ctx.Web.GetFolderByServerRelativePath($folder.ServerRelativeUrl)
$ctx.Load($fld.Folders)
$ctx.ExecuteQuery()
$folders += $fld.Folders | Select-Object Name, ServerRelativeUrl
return $folders | Out-GridView -Title "Select Folder" -PassThru
}
catch {
$script:txtStatus.Text += "Error browsing folders: $_`n"
}
}