Reputation: 1095
I'm trying to upload a file into a Sharepoint Online (M365) library, but it keeps giving me errors. I have tried many scripts. This post is about using Microsoft.SharePoint.Client.ClientContext (I have posted questions about other scripts hoping someone can help me with any of them)
This is the code:
$WebUrl = "https://myDomain.sharepoint.com/sites/mySite/"
$LibraryName ="myLibrary"
$SourceFile="C:\myFolder\myFile.csv"
$AdminName ="[email protected]"
$AdminPassword ="myPassword"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Context.Credentials = $Credentials
$Library = $Context.Web.Lists.GetByTitle($LibraryName)
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFile -leaf
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($FileUploaded)
$Context.ExecuteQuery()
$FileStream.Close()
Just from the beggining, I have this error:
New-Object : Cannot find type [Microsoft.SharePoint.Client.SharePointOnlineCredentials]: make sure the assembly containing this type is loaded.
I suppose I must load them. I have seen this code:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
But I don't have installed such libraries
Curiously, if I, previously, execute this command (just execute, and cancel prompt asking for URL afterwards):
Connect-PnPOnline
Then, somehow, the references are loaded, because the script then works flawlesly.
Could you tell me how to load those libraries?
Upvotes: 1
Views: 20409
Reputation: 1095
Finally I got this script working!!
First of all, you have to install SharePoint Online Management Shell:
Basically, execute:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
It will prompt you to install NuGet package, of not installed
Then, locate your libraries, and add path at the begining. In my case:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\Microsoft.Online.SharePoint.PowerShell\16.0.21411.12000\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\Microsoft.Online.SharePoint.PowerShell\16.0.21411.12000\Microsoft.SharePoint.Client.Runtime.dll"
And the script works like charm :)
Note: I got this script from:
https://www.sharepointdiary.com/2016/06/upload-files-to-sharepoint-online-using-powershell.html
Upvotes: 1