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.
Post Reply
User avatar
gijsbert
Posts: 2
Joined: Fri Oct 17, 2014 2:41 am

Upload a file to Sharepoint

Post by gijsbert » Tue Aug 13, 2019 7:12 am

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:

User avatar
jvierra
Posts: 13681
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Upload a file to Sharepoint

Post by jvierra » Tue Aug 13, 2019 7:27 am

The error tells you what the problem is. The "$file" argument is invalid.

User avatar
Alexander Riedel
Posts: 7001
Joined: Tue May 29, 2007 4:43 pm

Re: Upload a file to Sharepoint

Post by Alexander Riedel » Tue Aug 13, 2019 7:47 am

Probably works in the ISE because $File is contaminated from
something else but not initialized in the script.
Alexander Riedel
SAPIEN Technologies, Inc.

Post Reply