我正试图将一个文件上传到Sharepoint (M365)库中,但它总是给我带来错误。我试过很多剧本。这篇文章是关于使用Microsoft.SharePoint.Client.ClientContext的(我已经发布了关于其他脚本的问题,希望有人能帮助我处理其中的任何一个)。
这是代码:
$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()从乞丐的角度看,我犯了一个错误:
新对象:无法找到类型Microsoft.SharePoint.Client.SharePointOnlineCredentials:,请确保加载了包含此类型的程序集。
我想我必须装上它们。我见过这样的代码:
#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"但我没有安装这样的库
奇怪的是,如果我以前执行这个命令(只需执行,然后取消请求URL的提示):
Connect-PnPOnline然后,以某种方式加载引用,因为脚本运行得非常完美。
你能告诉我如何加载那些库吗?
发布于 2021-07-16 12:54:39
我终于让这个脚本生效了!
首先,您必须安装SharePoint联机命令行管理程序:
基本上,执行:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell它将提示您安装未安装的NuGet包。
然后,定位您的库,并在开始添加路径。就我而言:
#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"而剧本的效果就像魅力:)
注意:我从以下几个方面获得了这个脚本:
https://www.sharepointdiary.com/2016/06/upload-files-to-sharepoint-online-using-powershell.html
https://stackoverflow.com/questions/68408704
复制相似问题