简体   繁体   中英

Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 4 - Using Microsoft.SharePoint.Client.ClientContext)

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 ="mail@myDomain.com"
$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?

Finally I got this script working!!

First of all, you have to install SharePoint Online Management Shell:

https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-online/connect-sharepoint-online?view=sharepoint-ps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM