我是Powershell的新手,我正在尝试将文件从本地文件夹上传到Sharepoint。我似乎把文件放到库中,但没有进入我想要的第二个子文件夹。
到目前为止脚本:
#Specify tenant admin and site URL
$User = "admin@contoso.no"
$SiteURL = "https://contoso.sharepoint.com"
$Folder = "E:\LocalFolder"
$DocLibName = "Libraryname"
$FolderName = "Folder/SubFolder"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = "password" | ConvertTo-SecureString -AsPlainText -Force
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
#Retrieve folder
$FolderToBindTo = $List.RootFolder.Folders
$Context.Load($FolderToBindTo)
$Context.ExecuteQuery()
$FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $FolderName}
#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $FolderToUpload.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
如果我使用& FolderName =" Folder"脚本运行正常。但是如何将文件放入子文件夹呢?如果我将文件路径设置为FolderName则不起作用。我收到以下错误:
You cannot call a method on a null-valued expression.
+ $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Cannot find an overload for "Load" and the argument count: "1".
+ $Context.Load($Upload)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
enter code here
被困在这几天:(
答案 0 :(得分:1)
错误You cannot call a method on a null-valued expression.
发生,因为$FolderToUpload
对象未在行初始化:
$FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $FolderName}
当$FolderName
对象指向子文件夹名称
关键是$List.RootFolder.Folders
只返回位于列表或库下方一个级别的文件夹,因此无法以这种方式引用子文件夹。
相反,您可以考虑以下选项来引用子文件夹来添加文件。
FileCreationInformation.Url
属性使用FileCreationInformation.Url
property为上传的文件指定文件夹网址。
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
#Upload file(s)
Foreach ($File in (dir $Folder -File))
{
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
$FileCreationInfo.URL = $List.RootFolder.ServerRelativeUrl + "/" + $FolderName + "/" + $File.Name
$UploadFile = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($UploadFile)
$Context.ExecuteQuery()
}
Web.GetFolderByServerRelativeUrl
方法使用Web.GetFolderByServerRelativeUrl
method检索必须上传文件的文件夹:
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName);
#Upload file(s)
Foreach ($File in (dir $Folder -File))
{
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
$FileCreationInfo.URL = $File.Name
$UploadFile = $TargetFolder.Files.Add($FileCreationInfo)
$Context.Load($UploadFile)
$Context.ExecuteQuery()
}
答案 1 :(得分:0)
您还可以使用Office 365导入服务将文件批量上传到sharepoint,而不是使用CSOM脚本。
这使用新的Sharepoint API,它可以提供更快的速度,而不需要任何限制,因为它使用可扩展的azure基础设施。