Upload a file to Sharepoint

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
gijsbert
Posts: 3
Last visit: Wed Apr 10, 2024 12:36 am

Upload a file to Sharepoint

Post by gijsbert »

I wrote a powershell script to upload a file to sharepoint online.
The script works fine in PowerShell ISE, but throws an error when used in a powershell studio form. Can some one help me figure this out?

$FileStream = New-Object IO.FileStream($SourceFile.FullName, [System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()

throws error:
ERROR: Exception calling "Add" with "1" argument(s): "The 'parameters.Url' argument is invalid."
Test-OneDrive.psf (93, 5): ERROR: At Line: 93 char: 5
ERROR: + ... $Upload = $targetFolder.Files.Add($FileCreationInfo);
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : ArgumentException
ERROR:
ERROR: Cannot find an overload for "Load" and the argument count: "1".
Test-OneDrive.psf (97, 4): ERROR: At Line: 97 char: 4
ERROR: + $Context.Load($Upload)
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodException
ERROR: + FullyQualifiedErrorId : MethodCountCouldNotFindBest
ERROR:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Upload a file to Sharepoint

Post by jvierra »

The error tells you what the problem is. The "$file" argument is invalid.
User avatar
Alexander Riedel
Posts: 8488
Last visit: Mon Apr 15, 2024 3:28 pm
Answers: 20
Been upvoted: 37 times

Re: Upload a file to Sharepoint

Post by Alexander Riedel »

Probably works in the ISE because $File is contaminated from
something else but not initialized in the script.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 4 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked