Upload File to SharePoint in 2.5.4

Product: PowerShell Universal
Version: 2.5.4

I am working on uploading a file in the newer v3 dashboard directly to SharePoint. There have been a number of things resolved lately that help me move in this direction (thank you @adam for fixing the PNP.Powershell issues!), but I seem to still be missing something.

Here is my code:

New-UDForm -Id “UploadFile$InvoicesPKID” -Content {

$PMArray = $PMSQL | Sort-Object PMName

$PMArray += [pscustomobject]@{

    PMName = "None"

    ProjectManagementPKID = "9999999999"

    }

New-UDSelect -Id "DocType" -Label "Document Type" -Option {

    foreach ($type in $DLTypeSQL) {

        New-UDSelectOption -Name $type.DLType -Value $type.DLType

    }

}

New-UDUpload -ID "File" -Text "File"

New-UDTextBox -Id "Description" -Label "File Description"

New-UDSelect -Id "ProjectManagement" -Label "Associated Project" -Option {

    foreach ($pm in $PMArray) {

        New-UDSelectOption -Name $pm.PMName -Value $pm.ProjectManagementPKID

    }

} -DefaultValue "9999999999"

} -OnSubmit {

$DocType = $EventData.DocType

$File = $EventData.File

$Description = $EventData.Description

$ProjectManagement = $EventData.ProjectManagement

If ($ProjectManagement -eq "9999999999") {

    $ProjectManagement = $null

}

$FileName = $File.Name

Show-UDToast -Message "Filename: $FileName" -Duration 15000

Import-Module PnP.PowerShell

Connect-O365Service -SharePointPNP "https://myhost.sharepoint.com/sites/THQCentral"

$Upload = Add-PnPFile -Path $($File.Data) -Folder “Application Inventory/$DocType” -Values @{ VendorName = “$VendorName”; Description = “$Description”; Title = $($File.Name); Editor = $User; Author = $User; }

$UniqueID = $upload.UniqueId

Show-UDToast -Message "Uploaded $FileName to SharePoint library with ID of $UniqueID" -Duration 15000

}

When I do this, I get the error message “Local file was not found.”

What am I missing in this code?

You should be able to access the full file name of the upload using the $File.FileName parameter. Data is the base64 content of the file.

$Upload = Add-PnPFile -Path $($File.FileName) -Folder “Application Inventory/$DocType” -Values @{ VendorName = “$VendorName”; Description = “$Description”; Title = $($File.Name); Editor = $User; Author = $User; }

Hmm, still doesn’t seem to be working. Here are the error messages I get now:

Can we try to get a little more information about what the $File variable looks like in your environment?

Try:

$File | ConvertTo-Json | Out-File C:\Somepath.txt

It just seems like properties of the file are missing and I’m not sure why. If it doesn’t have the file name, then it didn’t write the file down correctly.

Here is the output:
{
“name”: “Invoice 748 - 2021 - 08 - USS.xlsx”,
“type”: “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,
“data”: “UEsDBBQABgAIAAAAIQCyI5feuQEAANkHAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA…”
}

I’ve truncated the data output…

Man that’s weird. Ok. Since you actually do have the data, try this.

$FileData = [Convert]::FromBase64String($File.Data)
$FilePath = [IO.Path]::GetTempFileName()
[IO.File]::WriteAllBytes($FilePath, $FileData)
$Upload = Add-PnPFile -Path $FilePath -Folder “Application Inventory/$DocType” -Values @{ VendorName = “$VendorName”; Description = “$Description”; Title = $($File.Name); Editor = $User; Author = $User; }

OK. This definitely got me close. I just had to change it from creating it with a temp file name (as that ended up being the file name on the upload to this:
$FilePath = “$env:temp$($FileName)”

Thanks so much for your help in figuring this out!