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.
User avatar
Posts: 2
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Fri Oct 17, 2014 2:41 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)

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: 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

Posts: 14444
Joined: Tue May 22, 2007 9:57 am

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: 7376
Joined: Tue May 29, 2007 4:43 pm

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.