我正试图将一个文件上传到Sharepoint Online(M365(库中,但它一直给我错误。我试过很多剧本。这篇文章是关于使用Microsoft.SharePoint.Client.ClientContext的(我发布了关于其他脚本的问题,希望有人能帮助我(
这是代码:
$WebUrl = "https://myDomain.sharepoint.com/sites/mySite/"
$LibraryName ="myLibrary"
$SourceFile="C:myFoldermyFile.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 FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
但我还没有安装这样的库
奇怪的是,如果我以前执行过这个命令(只执行,然后取消询问URL的提示(:
Connect-PnPOnline
然后,不知何故,引用被加载,因为脚本可以完美地工作。
你能告诉我如何加载那些库吗?
终于让这个脚本工作起来了!!
首先,您必须安装SharePoint Online Management Shell:
https://learn.microsoft.com/en-us/powershell/sharepoint/sharepoint-online/connect-sharepoint-online?view=sharepoint-ps
基本上,执行:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
它将提示您安装NuGet包,未安装
然后,找到您的库,并在开头添加路径。就我而言:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:Program FilesWindowsPowerShellModulesMicrosoft.Online.SharePoint.PowerShell16.0.21411.12000Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesWindowsPowerShellModulesMicrosoft.Online.SharePoint.PowerShell16.0.21411.12000Microsoft.SharePoint.Client.Runtime.dll"
剧本很有魅力:(
注意:我从获得了这个脚本
https://www.sharepointdiary.com/2016/06/upload-files-to-sharepoint-online-using-powershell.html